Search code examples
pythonflaskrequestbinaryfiles

Flask : get multiple form files from request


I have a python application running in flask, and I need to read multiple png image from a post request.

First, you can saw the request I try to attempt here (with the header : application/x-www-form-urlencoded as content-type) :

my request

what I want from my flask application is to get the binary data of each png file to archive them in tar file and upload in storage. Important : I don't know in advance how many files I will receive like that.

Regarding the doc (https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request) I have try different stuff, but don't find a good and easy way to get this binary data. For example I try :

dict_data = dict(request.form)
print(dict_data)

return me something like :

 {'------WebKitFormBoundaryfQXWRsfmCXS0xGpU\r\nContent-Disposition: form-data; name':'"file1"; filename="1.png"\r\nContent-Type: image/png\r\n\r\n�PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00�\x00\x00\x00�\x08\x06\x00\x00\x00�>a�\x00\x00 \x00IDATx^�{~#In��� ��k�7����\x7f�m���\x7f_\x04P�EQR�4�3�v�d��DVF\x06�\x00\x128���]_��]\x7f�LJ/\x0f\x00�9\x08\x1e\x00x\x00���\x7f�\x07\x03<\x00p�W��?��\x01\x1e\x00�� p�\x1f��\x00\x0f\x00��\x15���`�\x07\x00��\n���\x7f0�\x03\x00w~\x05���?\x18�\x01�;�\x02w��\x1f\x0c�\x00��_�;��\x0f\x06x\x00���\x7f�\x07\x03<\x00p�W��?��\x01\x1e\x00�� p�\x1f��\x00\x0f\x00��\x15���`�\x07\x00��\n���\x7f0�\x03\x00w~\x05���?\x18�\x01�;�\x02w��\x1f\x0c�\x00��_�;�
...
0f<)\x04\x00�����\x7f\x06E��\x08�[�n\x00\x00\x00\x00IEND�B`�\r\n------       WebKitFormBoundaryfQXWRsfmCXS0xGpU--\r\n': ''}

that is just impossible to parse. Same result for just :

request.form

which return :

ImmutableMultiDict([('------WebKitFormBoundaryYGznFIzweAgRIB3P\r\nContent-Disposition: form-data; name', '"file1"; filename="1.png"\r\nContent-Type: image/png\r\n\r\n�PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00�\x00\x00\x00�\x08\x06\x00\x00\x00�>a�\x00\x00 \x00IDATx^�{~#In��� ��k�7����\x7f�m���\x7f_\x04P�EQR�4�3�v�d��DVF\x06�\x00\x128���]_��]\x7f�LJ/\x0f\x00�9\x08\x1e\x00x\x00���\x7f�\x07\x03<\x00p�W��?��\x01\x1e\x00�� p�\x1f��\x00\x0f\x00��\x15���`�\x07\x00��\n��
...
Q\x06�/涮��\\>��c{\x05�D\x01)��r�EH��_<', ''), ('\t� ��v\x05\x00\x00���J��\x18���6\x15�a\x00�\x03�\x0f<)\x04\x00�����\x7f\x06E��\x08�[�n\x00\x00\x00\x00IEND�B`�\r\n------WebKitFormBoundaryYGznFIzweAgRIB3P--\r\n', '')])

And for the

request.files

I just have :

 ImmutableMultiDict([]) 

I just want a simple way to have something like :

{'file1': binary_data_of_1.png, 
'file2': binary_data_of_2.png}

Is there an easy way to have that in python ?

Thanks


Solution

  • If you really want the binary data of each file:

    @app.route("/upload", methods=["POST"])
    def upload():
        binary_data = {}
        the_files = request.files
        for file in the_files:
            binary_data[file] = the_files[file].read()
        ...
    

    The object the_files[file] is of type werkzeug.datastructures.FileStorage

    See Request.Files docs here

    See Datastructure of FileStorage here