Skip to content

Instantly share code, notes, and snippets.

@f9n
Created March 13, 2022 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f9n/20c2d900c92f3a8f38785c420d0c3214 to your computer and use it in GitHub Desktop.
Save f9n/20c2d900c92f3a8f38785c420d0c3214 to your computer and use it in GitHub Desktop.
Python-Flask: Save the uploaded image to multiple storages
# Fork: https://pythonbasics.org/flask-upload-file/
# Issues
# - Empty request file object
from flask import Flask, render_template, request
from werkzeug import secure_filename
app = Flask(__name__)
@app.route('/upload')
def upload_file():
return render_template('upload.html')
@app.route('/uploader', methods = ['POST'])
def upload_file():
f = request.files['file']
# In here, the cursor of the file object went to the end of the file.
f.save(secure_filename(f.filename))
# We have to set the cursor of the file to the beginning of the file with seek() method.
f.seek(0)
# Then, write the file to datastore or different path
write_request_file_to_datastore("mongodb", fileobj=f)
return 'file uploaded successfully'
if __name__ == '__main__':
app.run(debug = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment