3

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.