客户端将multipart/form-data请求的文件部分的内容类型设置为错误 [英] Content type for file part of the multipart/form-data request is set wrong by the client

查看:319
本文介绍了客户端将multipart/form-data请求的文件部分的内容类型设置为错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下JavaScript和jQuery发送multipart/form-data:

I'm trying to send multipart/form-data with following JavaScript and jQuery:

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

但是,某些客户端浏览器(Firefox和Chrome)从服务器接收400 Bad Request.在检查标头和请求有效负载时,我发现某些浏览器为文件设置了显式的内容类型,如下所示:

However, some client browsers (Firefox and Chrome) receive 400 Bad Request from the server. 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

在工作请求中,Content-Type应该如下所示:Content-Type: application/octet-stream,服务器可以正确处理.

In a working request the Content-Type should be as follows: Content-Type: application/octet-stream, which the server can handle properly.

我怀疑这与浏览器的配置或文件关联有关.有没有一种方法可以为请求的文件部分显式设置内容类型?

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?

使用Firefox和Chrome的某些用户会出现此问题.但是,某些用户可以使用Chrome和Firefox成功上传.我们的应用程序不支持IE.

The problem occurs with some users using Firefox and Chrome. However, some users are able to upload succesfully using Chrome and Firefox. IE is not supported by our application.

推荐答案

好的,我们设法弄清了这个问题.事实证明,在某些安装了某些第三方应用程序的客户端计算机上,注册到客户端系统的内容类型实际上是错误的.

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.

我们无法以编程方式更改零件的浏览器集的内容类型. 正如Michael-O所指出的,您应该始终使用在 IANA .这是与标准的链接.

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. Here's a link to the standard.

在这种情况下,第三方软件向客户的系统注册了非法的内容类型.内容类型不能包含空格,因此内容类型EXT Project Data显然是非法的.通过将注册的内容类型更改为自定义内容类型,我们解决了该问题.因此,我们现在使用的内容类型是application/x-ext-project-data,然后可以在服务器端对其进行正确处理.

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. 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.

这篇关于客户端将multipart/form-data请求的文件部分的内容类型设置为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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