Flask: How to handle application/octet-stream

前端 未结 2 1926
甜味超标
甜味超标 2021-02-08 13:06

I want to make a multiple file-upload form.I use jQuery File Uploader. My server-side code:

@app.route(\"/new/photogallery\",methods=[\"POST\"])
def newPhotoGall         


        
2条回答
  •  -上瘾入骨i
    2021-02-08 13:34

    Regardless of the the data encoding, you should be able to get the raw data with request.data. In the case of application/octet-stream, you can just write request.data to a binary file.

    An example handler for various data types:

    from flask import json
    
    @app.route('/messages', methods = ['POST'])
    def api_message():
    
        if request.headers['Content-Type'] == 'text/plain':
            return "Text Message: " + request.data
    
        elif request.headers['Content-Type'] == 'application/json':
            return "JSON Message: " + json.dumps(request.json)
    
        elif request.headers['Content-Type'] == 'application/octet-stream':
            with open('/tmp/binary', 'wb') as f:
                f.write(request.data)
                f.close()
            return "Binary message written!"
    
        else:
            return "415 Unsupported Media Type ;)"
    

    The typical scenario of handling form data is already documented here.

提交回复
热议问题