How to disable base64 encoded file names in HttpClient / MultipartFormDataContent

I am using HttpClientPOST MultipartFormDataContentfor a Java web application. I upload several StringContentsand one file, which I add as StreamContent, using MultipartFormDataContent.Add(HttpContent content, String name, String fileName), using the method HttpClient.PostAsync(String, HttpContent).

This works fine except when I provide a filename containing German umlauts (I have not tested other non-ASCII characters yet). In this case, filename is encoded with base64. Result for a file named 99 2 LD 353 Temp Äüöß-1.txt

looks like that:

 __utf-8_B_VGVtcCDvv73vv73vv73vv71cOTkgMiBMRCAzNTMgVGVtcCDvv73vv73vv73vv70tMS50eHQ___

The Java server displays this encoded file name in its user interface, which confuses users. I cannot change server changes.

How to disable this behavior? Any help would be greatly appreciated.

Thanks in advance!

+3
source share
4 answers

I finally gave up and solved the problem using HttpWebRequestinstead HttpClient. I had to manually create headers and content, but that allowed me to ignore the standards for sending non-ASCII file names. I ended up recoding unencoded UTF-8 file names to the header filename, which was the only way the server would agree with my request.

+1
source

I just found the same restriction as StrezzOr, because the server I was consuming did not follow the standard file name.

UTF-8 "" ( UTF-8).

:

        FileStream fs = File.OpenRead(_fullPath);
        StreamContent streamContent = new StreamContent(fs);
        streamContent.Headers.Add("Content-Type", "application/octet-stream");
        String headerValue = "form-data; name=\"Filedata\"; filename=\"" + _Filename + "\"";
        byte[] bytes = Encoding.UTF8.GetBytes(headerValue);
        headerValue="";
        foreach (byte b in bytes)
        {
            headerValue += (Char)b;
        }
        streamContent.Headers.Add("Content-Disposition", headerValue);
        multipart.Add(streamContent, "Filedata", _Filename);

.

, .

+5

, :

:

private static readonly Regex _regexEncodedFileName = new Regex(@"^=\?utf-8\?B\?([a-zA-Z0-9/+]+={0,2})\?=$");

private static string TryToGetOriginalFileName(string fileNameInput) {
    Match match = _regexEncodedFileName.Match(fileNameInput);
    if (match.Success && match.Groups.Count > 1) {
        string base64 = match.Groups[1].Value;
        try {
            byte[] data = Convert.FromBase64String(base64);
            return Encoding.UTF8.GetString(data);
        }
        catch (Exception) {
            //ignored
            return fileNameInput;
        }
    }
    return fileNameInput;
}

:

string correctedFileName = TryToGetOriginalFileName(fileRequest.FileName);

.

+1

-ascii Content-Disposition, filename* filename. . .

HttpClient :

   var streamcontent = new StreamContent(stream);
   streamcontent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
      FileNameStar = "99 2 LD 353 Temp Äüöß-1.txt" 
   };
   multipartContent.Add(streamcontent);

:

  Content-Disposition: attachment; filename*=utf-8''99%202%20LD%20353%20Temp%20%C3%84%C3%BC%C3%B6%C3%9F-1.txt
0

All Articles