FileStorage contains stream field. This object must extend IO or file object, so it must contain read and other similar methods. FileStorage also extend stream field object attributes, so you can just use file.read() instead file.stream.read(). Also you can use save argument with dst parameter as StringIO or other IO or file object to copy FileStorage.stream to another IO or file object.
See documentation: http://flask.pocoo.org/docs/api/#flask.Request.files and http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.FileStorage.
Answer from tbicr on Stack Overflowpython - How to do POST using requests module with Flask server? - Stack Overflow
[Ask Flask] How to receive and save a file?
python - Flask request.files is empty - Stack Overflow
python - Flask request.files is not in 'file' - Stack Overflow
Videos
FileStorage contains stream field. This object must extend IO or file object, so it must contain read and other similar methods. FileStorage also extend stream field object attributes, so you can just use file.read() instead file.stream.read(). Also you can use save argument with dst parameter as StringIO or other IO or file object to copy FileStorage.stream to another IO or file object.
See documentation: http://flask.pocoo.org/docs/api/#flask.Request.files and http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.FileStorage.
If you want to use standard Flask stuff - there's no way to avoid saving a temporary file if the uploaded file size is > 500kb. If it's smaller than 500kb - it will use "BytesIO", which stores the file content in memory, and if it's more than 500kb - it stores the contents in TemporaryFile() (as stated in the werkzeug documentation). In both cases your script will block until the entirety of uploaded file is received.
The easiest way to work around this that I have found is:
1) Create your own file-like IO class where you do all the processing of the incoming data
2) In your script, override Request class with your own:
class MyRequest( Request ):
def _get_file_stream( self, total_content_length, content_type, filename=None, content_length=None ):
return MyAwesomeIO( filename, 'w' )
3) Replace Flask's request_class with your own:
app.request_class = MyRequest
4) Go have some beer :)
Because you have <input> with name=file so you need
files={'file': ('random.txt', open('random.txt', 'rb'))}
Examples in requests doc: POST a Multipart-Encoded File
You upload the file as the random.txt field:
files={'random.txt': open('random.txt', 'rb')}
# ^^^^^^^^^^^^ this is the field name
but look for a field named file instead:
file = request.files['file']
# ^^^^^^ the field name
Make those two match; using file for the files dictionary, for example:
files={'file': open('random.txt', 'rb')}
Note that requests will automatically detect the filename for that open fileobject and include it in the part headers.
Hi I'm using react native frontend and flask backend. I am trying to send an audio file from the phone to the server so the server can save it. For image uploads I encode the image as base64 string, send it and on Flask I decode it and save the image and everything works fine. But for the audio the react native gives me basically just the URI on the phone, and all the tutorials are saying that I should send it with FormData in format like
var formData =new FormData();
formData.append('file', JSON.stringify({
name: filename,
filename: filename+'.mp4',
filepath: fsPath,
filetype: 'audio/x-mp4'
})
}But I am confused how I am supposed to actually download or get the file once I receive it in the Flask. All I have is a JSON object with filename, type, and the internal phone filepath. I have no idea how to actually convert that into some data that I can save as a file.
The Flask tutorial has an article about how to upload files with HTML <form inputType=file> but they receive it as request.files[] whereas I receive it as request.form['file'], whereas since I'm doing everything in JS code everyone is saying that I should use FormData which is kind of making me confused whether I am not sending it correctly (I posted a question on reactnative sub as well) but also maybe I don't know how to receive it.
Thank you for any help!