PUT请求上载在Flask中不起作用的文件 [英] PUT request to upload a file not working in Flask

查看:231
本文介绍了PUT请求上载在Flask中不起作用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask开发Web应用程序.其中一种视图应该通过PUT请求接受上载的文件,但是我只能使用$ curl -F upload=@filename URL来获取POST请求才能正常工作.对于诸如$ curl --upload-file filenname URL的PUT请求,request.files ImmutableMultiDict为空.我是否在Flask中缺少某些东西,或者可能是使用curl?

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?

推荐答案

PUT请求与POST请求相比有很大不同.通过PUT请求,可以使用 request.data request.stream .第一个将输入的数据存储为字符串,而request.stream的行为更像文件对象,使其更适合于二进制数据:

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())

这篇关于PUT请求上载在Flask中不起作用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆