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?