Check Flask upload if user did not select file

If the user does not select the file in the form (equals PHP UPLOAD_ERR_NO_FILE ), it request.files['file']also returns the object FileStorageI use request.files['file'].filename == ''to check if it is better? I saw the Flask Doc but cannot find the answer.

'file' in request.files will not work for the user, does not select the file, the browser will also send 0 length and the file name is equal to '' (empty line).

and how can I detect the downloaded file, the error was only partially downloaded (equal to PHP UPLOAD_ERR_PARTIAL )?

+4
source share
2 answers

Now i use

if request.files['file'].filename == '':
    return 'No selected file'

or by checking file length

import os

file = request.files['file']
file.seek(0, os.SEEK_END)
if file.tell() == 0:
    return 'No selected file'
+5

Try:

if not request.files.get('file', None):
    pass

, http://pythonhosted.org/Flask-Uploads/

+3

Source: https://habr.com/ru/post/1540155/


All Articles