简体   繁体   English

Django中的空request.FILES

[英]Empty request.FILES in Django

I'm trying to send a file via post request to server on localhost. 我正在尝试通过发布请求将文件发送到localhost上的服务器。 I'm using HttpRequester in Firefox (also tried Postman in Chrome and Tasker on Android) to sumbit request. 我在Firefox中使用HttpRequester(也在Chrome中尝试过Postman,在Android上也尝试过Tasker)来汇总请求。 The problem is that request.FILES is always empty. 问题是request.FILES始终为空。 But when I try to print request.body it shows some non-human-readable data which particularly include the data from the file I want to upload (it's a database). 但是,当我尝试打印request.body它会显示一些非人类可读的数据,尤其是包含我要上传的文件(它是数据库)中的数据。 So it makes sense to me that somehow file arrives to the server. 因此,对于我来说,文件以某种方式到达服务器很有意义。

From Django docs: 从Django文档中:

Note that FILES will only contain data if the request method was POST and the that posted to the request had enctype="multipart/form-data". 请注意,只有在请求方法为POST且发布到请求的方法为enctype =“ multipart / form-data”时,FILES才会包含数据。 Otherwise, FILES will be a blank dictionary-like object. 否则,FILES将是空白的类似字典的对象。

There was an error 'Invalid boundary in multipart: None' when I tried to set Content-type of request to 'multipart/form-data'. 当我尝试将请求的Content-type设置为“ multipart / form-data”时,出现错误“ multipart中的无效边界:无 ”。 An error disappeared when I added ';boundary=frontier' to Content-type. 当我在Content-type中添加'; boundary = frontier'时,错误消失了。

Another approach was to set enctype="multipart/form-data". 另一种方法是设置enctype =“ multipart / form-data”。

Therefore I have several questions: 因此,我有几个问题:

  1. Should I use exactly multipart/form-data content-type? 我应该使用完全为multipart / form-data的内容类型吗?
  2. Where can I specify enctype? 在哪里可以指定enctype? (headers, parameters, etc) (标题,参数等)
  3. Why the file's data contains in request.body but request.FILES is empty? 为什么文件的数据包含在request.body中,但request.FILES为空?

Thanks 谢谢

  1. Should I use exactly multipart/form-data content-type? 我应该使用完全为multipart / form-data的内容类型吗?

Django supports only multipart/form-data , so you must use that content-type. Django仅支持multipart/form-data ,因此您必须使用该内容类型。

  1. Where can I specify enctype? 在哪里可以指定enctype? (headers, parameters, etc) (标题,参数等)

in normal HTML just put enctype="multipart/form-data" as one of parameters of your form element. 在普通HTML中,只需将enctype="multipart/form-data"用作表单元素的参数之一。 In HttpRequester it's more complicated, because I think it lacks support for multipart/form-data by default. 在HttpRequester中,它更为复杂,因为我认为默认情况下它不支持multipart/form-data http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2 is more details about multipart/form-data , it should be possible to run it in HttpRequester by hand. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2是有关multipart/form-data更多详细信息,应该可以在HttpRequester中手动运行它。

  1. Why the file's data contains in request.body but request.FILES is empty? 为什么文件的数据包含在request.body中,但request.FILES为空?

You've already answered that: 您已经回答了:

Note that FILES will only contain data if the request method was POST and the that posted to the request had enctype="multipart/form-data". 请注意,只有在请求方法为POST且发布到请求的方法为enctype =“ multipart / form-data”时,FILES才会包含数据。 Otherwise, FILES will be a blank dictionary-like object. 否则,FILES将是空白的类似字典的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM