Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Flask upload if user does not selected file

If user doesn't select a file in form (equals PHP UPLOAD_ERR_NO_FILE ), request.files['file'] also return a FileStorage object, I am using request.files['file'].filename == '' to check, any better way ? I have seen Flask Doc but can't find the answer.

'file' in request.files will not work on user doesn't select file, browser also will submit a 0 length and filename equals '' (empty string) part.

and how do I detect uploaded file was only partially uploaded error (equals PHP UPLOAD_ERR_PARTIAL) ?

like image 394
Steely Wing Avatar asked May 12 '14 02:05

Steely Wing


1 Answers

Now I am using

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

or using file length check

import os

file = request.files['file']
file.seek(0, os.SEEK_END)
if file.tell() == 0:
    return 'No selected file'
like image 94
Steely Wing Avatar answered Sep 27 '22 21:09

Steely Wing