C# 非ascii字符的双编码文件名

C# 非ascii字符的双编码文件名,c#,encoding,http-headers,content-disposition,httppostedfile,C#,Encoding,Http Headers,Content Disposition,Httppostedfile,我的内容配置中的文件名是mime/quoted可打印编码的,但HttpContext.Current.Request.Files没有解码该值,而是得到了“文件名”,如: =?utf-8?B?ZM9VIOKAKYBIYBXNN= 它应该说“foo–bar.msg” wireshark捕获的内容处置为: 表格数据;名称=\“文件\”;文件名=\“=?utf-8?B?ZM9VIOKAKYBIYZUIBXNN?=\” 我的客户代码: string address = "http://localhost/

我的
内容配置
中的文件名是mime/quoted可打印编码的,但
HttpContext.Current.Request.Files
没有解码该值,而是得到了“文件名”,如:

=?utf-8?B?ZM9VIOKAKYBIYBXNN=

它应该说“foo–bar.msg”

wireshark捕获的内容处置为:

表格数据;名称=\“文件\”;文件名=\“=?utf-8?B?ZM9VIOKAKYBIYZUIBXNN?=\”

我的客户代码:

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
的实例?

我在尝试从具有特殊字符名称的文件中获取文件名时遇到了相同的问题,在我的例子中使用了重音符号

我的解决方案是,在服务器端:

  • 拆下此零件=?utf-8?B?ZM9VIOKAKYBIYXUIBXNN=
  • 结果ZM9VIOKAKYBIYXUIBXNN是一个base64字符串,使用
    Convert.FromBase64String
    获取一个字节[]->
    var myBytes=Convert.FromBase64String(“ZM9VIOKAKYBIYXUIBXNN”)
  • 将字节解码为UTF8字符串:
    string utf8String=Encoding.UTF8.GetString(myBytes),您将获得原始文件名
  • 可选:从结果字符串中删除变音重音
  • PD:

    Esto sirve para obtener
    httpPostedFile.FileName
    de archivos con nombre con characteres 特别是阿森托斯

    问候