I am trying to use HTML5 <input type="file"/> to upload a file to a server running Flask.
There is a decent article explaining how to do this. However, it suggests getting the data that’s been sent to the server using:
PHP: $_SERVER['HTTP_X_FILE_NAME'] Rails: request.env['HTTP_X_FILE_NAME'] Django: request.META['HTTP_X_FILE_NAME']
Unfortunately I do not know an equivalent for the above in Flask. Based on my reading I tried:
- request.data
- request.stream.read()
- request.input_stream.read()
- request.input_stream.read(
request.headers.get(‘content-type’, type=int) or 0)
Unfortunately none seemed to work as expected.
I am using Chrome to upload the file with:
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload/uri', true);
xhr.send(file);
And in Chrome, after I initiate a transfer, under Headers of the Network transfer of this given POST in the Developer Tools the Content-Length is set to exactly the size of the file being uploaded – which suggests that the file is actually being sent.
I would like to know how to access the file being sent in Flask, or alternatively how one might prefer to send HTML5 files to a server.
Have you seen the update to the article you reference? You can use
FormDatanow and avoid having to deal with AJAX uploads differently than browser ones:Quoting from the article: