简体   繁体   English

客户端设置了multipart / form-data请求的文件部分的内容类型错误

[英]Content type for file part of the multipart/form-data request is set wrong by the client

I'm trying to send multipart/form-data with following JavaScript and jQuery: 我正在尝试使用以下JavaScript和jQuery发送multipart/form-data

var formData = new FormData();

formData.append("projectName", $("#projectNameInput").val());

var file = $("#fileInput")[0].files[0];
formData.append("content", file);

var xhr = new XMLHttpRequest();
xhr.open('POST', '/project', true);
xhr.onload = function(ev) {
  // Handling logic omitted
};
xhr.send(formData);

However, some client browsers (Firefox and Chrome) receive 400 Bad Request from the server. 但是,某些客户端浏览器(Firefox和Chrome)从服务器收到400 Bad Request While examining the headers and request payload I discovered that some browsers set explicit content type for the file as follows: 在检查标头和请求有效负载时,我发现某些浏览器为文件设置了显式内容类型,如下所示:

------WebKitFormBoundaryEuDIpEU2Ci8VNwNJ 
Content-Disposition: form-data; name="content"; filename="testfile.ext" 
Content-Type: EXT Project Data (64bit)

------WebKitFormBoundaryEuDIpEU2Ci8VNwNJ

In a working request the Content-Type should be as follows: Content-Type: application/octet-stream , which the server can handle properly. 在工作请求中, Content-Type应如下所示: Content-Type: application/octet-stream ,服务器可以正确处理。

I suspect this has something to do with browser's configuration or file associations. 我怀疑这与浏览器的配置或文件关联有关。 Is there a way to explicitly set the content type for the file part of the request? 有没有办法明确设置请求的文件部分的内容类型?

The problem occurs with some users using Firefox and Chrome. 某些使用Firefox和Chrome的用户会出现此问题。 However, some users are able to upload succesfully using Chrome and Firefox. 但是,有些用户可以使用Chrome和Firefox成功上传。 IE is not supported by our application. 我们的应用程序不支持IE。

Ok, we managed to figure out this issue. 好的,我们设法解决了这个问题。 It turned out that the content type registered to the client system was actually malformed on some client machines, that had certain third-party application installed. 事实证明,在某些客户端计算机上注册到客户端系统的内容类型实际上是格式错误的,这些客户端计算机上安装了某些第三方应用程序。

We cannot programmatically change the content type browser sets for the part. 我们无法以编程方式更改该部件的内容类型浏览器集。 As Michael-O pointed out, you should always use content-types registered with the IANA . 正如Michael-O指出的那样,您应该始终使用在IANA注册的内容类型。 Here's a link to the standard. 这是标准的链接

In this case it was third-party software that registered illegal content type to client's system. 在这种情况下,第三方软件向客户的系统注册了非法内容类型。 Content type may not contain white spaces, so the content type EXT Project Data is clearly illegal. 内容类型可能不包含空格,因此内容类型EXT Project Data显然是非法的。 We fixed the issue by changing the registered content type to a custom content type . 我们通过将注册的内容类型更改为自定义内容类型来解决此问题。 So the content type we are now using is application/x-ext-project-data , which is then handled properly on the server side. 所以我们现在使用的内容类型是application/x-ext-project-data ,然后在服务器端正确处理。

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

相关问题 请求不包含多部分/表单数据或多部分/混合流,内容类型标头为false - the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is false 如何调用$ .post并设置内容类型Multipart / form-data数据 - How call $.post and set content type Multipart/form-data data 如何从multipart / form-data正文请求中删除Content-Type? - How to remove Content-Type from multipart/form-data Body Request? 在XHR中使用multipart / form-data作为Content-Type时获得'400 Bad Request' - Getting '400 Bad Request' when using multipart/form-data as Content-Type in XHR 如何提取通过 multipart/form-data 发送的文件的 Content-Type - How to extract the Content-Type of a file sent via multipart/form-data 在Angular2中构建multipart / form-data POST请求并验证输入类型File - Build multipart/form-data POST request in Angular2 and validate Input type File 如何设置POST的MIME类型-axios中的multipart / form-data? - How to set MIME type for POST - multipart/form-data in axios? 使用 axios 在 POST 多部分/表单数据请求中发送文件和 json - sending file and json in POST multipart/form-data request with axios Request.js模拟文件上传(多部分/表单数据) - Request.js Simulate a File Upload (multipart/form-data) 将收到的文件发送到 axios multipart/form-data 请求 - Send received file to an axios multipart/form-data request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM