Created
March 13, 2022 19:26
-
-
Save f9n/20c2d900c92f3a8f38785c420d0c3214 to your computer and use it in GitHub Desktop.
Python-Flask: Save the uploaded image to multiple storages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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