1

I am having an issue with the filename in my Content-Disposition being mime/quoted-printable encoded but HttpContext.Current.Request.Files is not decoding the value, instead i get 'filenames' like:

=?utf-8?B?Zm9vIOKAkyBiYXIubXNn?=

It should say "foo – bar.msg"

The wireshark captured Content-Disposition is:

form-data;name=\"file\";filename=\"=?utf-8?B?Zm9vIOKAkyBiYXIubXNn?=\"

My client code:

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

My server code

public void Post()
{
    // this line results in filename being set to the encoded value
    string filename = HttpContext.Current.Request.Files[0].FileName;
}

Is there any way to get the HttpFileCollection to decode the values? or more likely, is there a way to prevent my client code from double-encoding the value?

Because the content-disposition is in the multi-part boundary section I cannot use Request.Content.Headers.ContentDisposition as it is null? Is there a way to get an instance of ContentDispositionHeaderValue from the body of a multi-part form data request?

Seph
  • 8,472
  • 10
  • 63
  • 94

1 Answers1

0

I had the same problem when try to get the FileName from files with name with special chars, in my case using accents.

My solution was, in server side:

  1. Remove this parts =?utf-8?B? Zm9vIOKAkyBiYXIubXNn ?=
  2. The result Zm9vIOKAkyBiYXIubXNn is a base64 string, use Convert.FromBase64String to get a byte[] -> var myBytes = Convert.FromBase64String("Zm9vIOKAkyBiYXIubXNn");
  3. Decode bytes into a UTF8 string: string utf8String = Encoding.UTF8.GetString(myBytes);, and you will get your original file name
  4. Optional: remove diacritical accents from your result string

PD:

Esto sirve para obtener httpPostedFile.FileName de archivos con nombre con caracteres especiales o acentos.

Regards!

tec-net
  • 26
  • 5