简体   繁体   中英

File not uploading in flask

main.py

@app.route('/', methods=['GET', 'POST'])
def hello_world():
    if request.method == "POST":
        userFile = request.files('uFile')
        fileName = secure_filename(userFile.filename)
        userFile.save(path.join(app.config['UPLOAD_FOLDER'], fileName))
        return "Uploaded"

index.html

 <form action="/" method="POST">
            <div class="mb-3">
                <label for="formFile" class="form-label"></label>
                <input class="form-control" type="file" id="uFile" name="uFile">
            </div>

Error:

TypeError: 'ImmutableMultiDict' object is not callable
File "C:\Users\Shakti Ranjan Debata\Desktop\Minimal\ImageFilter\main.py", line 15, in hello_world
userFile = request.files('uFile')

I believe you should use brackets for the request:

        userFile = request.files['uFile']

The problem seems to be with your main.py, not your index.html. Flask needs you to use square brackets [] when handling requests. In main.py replace that route with:

@app.route('/', methods=['GET', 'POST'])
def hello_world():
  if request.method == "POST":
    userFile = request.files['uFile']
    fileName = secure_filename(userFile.filename)
    userFile.save(path.join(app.config['UPLOAD_FOLDER'], fileName))
    return "Uploaded"

That's all, and your issues should be solved. Flask doesn't allow you to use ordinary brackets to handle requests. I hope this helps, and happy coding!

Change "(" ")" to square brackets. In python dict accessing is done through square brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM