0

I am using ASP.NET server on .NET 4.5 and client is C# HttpClient on WinRT platform. I want to upload files using the HttpClient and used System.Net.Http.MultipartFormDataContent class to construct a valid http request. Everything worked fine until I had a filename with DBCS characters.

MultiPartFormDataContent class correctly encodes characters in the uploaded filename and sends both filename and filename* keys as per RFC 6266 in the content disposition header.

However, ASP.NET server ignores the filename* and read filename only and hence the file gets saved on the server with weird characters.

Has someone else faced the same problem? How can I get filename* at the server end and ignore filename key from the HttpRequest? [This would be my preferred solution. ]

Alternatively, how can I force MultiPartFormDataContent to send filename key only and force set UTF-8 encoded string?

2 Answers2

1

Add a reference to System.Net.Http and do something like below...

string suggestedFileName;   
string dispositionString = response.GetResponseHeader("Content-Disposition");

    if (dispositionString.StartsWith("attachment")) {
          System.Net.Http.Headers.ContentDispositionHeaderValue contentDisposition = System.Net.Http.Headers.ContentDispositionHeaderValue.Parse(dispositionString);
          if (!string.IsNullOrEmpty(contentDisposition.FileNameStar))
          {
              suggestedFileName = contentDisposition.FileNameStar;
          }
          else
          {
               suggestedFileName = contentDisposition.FileName.Trim('"');
          }

         }

ContentDispositionHeaderValue From Microsoft

Edwin
  • 1,458
  • 10
  • 17
0

Late to the party..

With control over both the client and server, my (dirty) workaround was simply to always Base64-encode the filename in HttpClient when creating the content, and decode it again on the server side. This way you avoid having to deal with the aptly named FileNameStar.

You could also try manually detecting the FileName encoding and decode it on the server. Related thread: System.Net.Mail and =?utf-8?B?XXXXX.... Headers

Community
  • 1
  • 1
NinjaCarr
  • 23
  • 5