Read-in Files From Flask Request Module
Solution 1:
if 'file' in request.files:
This is looking for the field name 'file'
which corresponds to the name
attribute you set in the form:
<input type='file' name='file'>
You then need to do something like this to assign the FileStorage
object to the variable mem
:
mem = request.files['file']
See my recent answer for more details of how and why.
You can then access the filename itself with:
mem.filename # should give back 'my_file.xls'
To actually read the stream data:
mem.read()
The official flask docs have further info on this, and how to save to disk with secure_filename()
etc. Probably worth a read.
All I want to do is read-in the incoming file in the request body, parse the contents and return the contents as a json body.
If you actually want to read the contents of that Excel file, then you'll need to use a library which has compatibility for this such as xlrd
. this answer demonstrates how to open a workbook, passing it as a stream. Note that they have used fileobj
as the variable name, instead of mem
.
Post a Comment for "Read-in Files From Flask Request Module"