gpt4 book ai didi

asp.net - Multipart和Form在客户端上传方面的区别

转载 作者:行者123 更新时间:2023-12-03 06:04:04 24 4
gpt4 key购买 nike

我使用 HttpClient 库创建了 C# 客户端。

我使用 BASE64 编码数据来上传文件(通过 POST)。有时,即使数据不太大(大约500kB),我也会遇到错误(可能是因为内容长度限制)。

我把它改成了MultipartFormData POST,正如我们所料,它在超过 1MB 的情况下运行正常。

Web 服务器对简单表单 POST 和多部分 POST 之间的 session 的处理方式是否不同?

请注意,我使用的 Web 服务是 Azure 网站。

最佳答案

这不是 Azure 或 IIS 中的任何限制。 HTTP协议(protocol)就是这样设计的!详细了解 Form 元素的不同内容类型 here .

来自规范:

application/x-www-form-urlencoded

This is the default content type. Forms submitted with this content type must be encoded as follows:

Control names and values are escaped. Space characters are replaced by +', and then reserved characters are escaped as described in
[RFC1738], section 2.2: Non-alphanumeric characters are replaced by
%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A')....

现在对于多部分:

multipart/form-data

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

The content "multipart/form-data" follows the rules of all multipart MIME data streams as outlined in [RFC2045]. The definition of "multipart/form-data" is available at the [IANA] registry.

因此,要上传文件,您应该始终使用multipart/form-data。不仅适用于 Azure,不仅适用于 IIS,还适用于任何托管提供商和任何实现 HTTP 协议(protocol)标准的 Web 服务器。

关于asp.net - Multipart和Form在客户端上传方面的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com