Python Load Image Checkbox

I am trying to upload an image to a folder on the system and give it a unique ID. Thus, each member can have their own profile profile. I'm having trouble assigning an ID and I'm not sure if I am going to do it right. Also, would it be better to put the image in SQLalchemy database or just in 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 mistake: 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 cannot be called

+3


source to share


1 answer


The correct way to write the file is:

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 must have UPLOAD_FOLDER

.

+2


source







All Articles