【发布时间】:2026-02-20 13:40:01
【问题描述】:
我的Content-Disposition 中的文件名被 mime/quoted-printable 编码时遇到问题,但HttpContext.Current.Request.Files 没有解码该值,而是得到类似“文件名”:
=?utf-8?B?Zm9vIOKAkyBiYXIubXNn?=
应该说“foo – bar.msg”
wireshark 捕获的 Content-Disposition 是:
form-data;name=\"file\";filename=\"=?utf-8?B?Zm9vIOKAkyBiYXIubXNn?=\"
我的客户代码:
string address = "http://localhost/test";
string filename = "foo – bar.msg";
Stream stream = File.Open(filename, FileMode.Open);
using (HttpClient client = new HttpClient())
{
// Create a stream content for the file
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition =
new ContentDispositionHeaderValue("form-data")
{
Name = "\"file\"",
FileName = filename
};
fileContent.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
content.Add(fileContent);
Uri requestAddress = new Uri(address);
// Post the MIME multipart form data upload with the file
HttpResponseMessage response =
client.PostAsync(requestAddress, content).Result;
}
}
我的服务器代码
public void Post()
{
// this line results in filename being set to the encoded value
string filename = HttpContext.Current.Request.Files[0].FileName;
}
有没有办法让HttpFileCollection 解码这些值?或者更有可能,有没有办法防止我的客户端代码对值进行双重编码?
因为内容配置位于多部分边界部分,我不能使用Request.Content.Headers.ContentDisposition,因为它是null?有没有办法从多部分表单数据请求的正文中获取ContentDispositionHeaderValue 的实例?
【问题讨论】:
标签: c# encoding http-headers content-disposition httppostedfile