PUT request to upload a file not working in Flask

蹲街弑〆低调 提交于 2019-12-13 11:43:40

问题


I am working on a web application using Flask. One of the views is supposed to accept uploaded files through PUT requests, however I only can get POST requests with $ curl -F upload=@filename URL to work properly. With PUT requests such as $ curl --upload-file filenname URL the request.files ImmutableMultiDict is empty. Am I missing something in Flask or maybe with using curl?


回答1:


PUT request is way different compared to POST request. With PUT request the file contents can be accessed using either request.data or request.stream. The first one stores incoming data as string, while request.stream acts more like a file object, making it more suitable for binary data:

with open('uploaded_image.jpg', 'w') as f:
    f.write(request.stream.read())


来源:https://stackoverflow.com/questions/9533462/put-request-to-upload-a-file-not-working-in-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!