Im trying to upload an image to a folder on the system and give it a unique identifier. That way each member can have there own profile image. Im having trouble assigning an id and not sure if im going about this correctly. Also, Would it be better to put the image in the SQLalchemy database or just a folder?
@main.route("/upload", methods=['GET', 'POST'])
@login_required
def upload():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
rec = file(filename=filename, user=g.user.id)
rec.store()
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('.home'))
return """
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
<p>%s</p>
""" % "<br>".join(os.listdir(app.config['UPLOAD_FOLDER'],))
my error: File "/home/ed/Development/Python/social/app/main/views.py", line 253, in upload rec = file(filename=filename, user=g.user.id) TypeError: 'FileStorage' object is not callable
The correct way to write the file would be:
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], actual_filename))
To use this, you should have an UPLOAD_FOLDER in your config.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With