1

I want to download a file with Japanese filename without change the originality. Here is the code which I am using for download a file:

Response.ContentType = "application/octet-stream";
var contentDisposition = "attachment; filename*=UTF-8''" + Uri.EscapeDataString("filename");
Response.Headers["Content-Disposition"] = contentDisposition;

return await _s3.DownloadFileAsStream(S3Storage.FilesBucket, filename); 

This will return the file with filenames:

High Siera.pdf --> High Siera.pdf (Correct)

2018年 国内カレンダー年 --> 2018年%E3%80%80国内カレンダー年 (Wrong)

Japanese filename getting escaped and replaced.

Matěj Štágl
  • 870
  • 1
  • 9
  • 27

2 Answers2

0

You get encoded value which is transformed during http communication. You can decode value before using. I hope this helps you. (HttpUtility in System.Web package)

string decodedFileName = HttpUtility.UrlDecode(filename);

return await _s3.DownloadFileAsStream(S3Storage.FilesBucket, decodedFileName); 
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25
  • Thanks for the response. from s3 I got the file correctly. But downloaded filename only having the issue. When giving attachment name in Content-Disposition, need to encode the Japanese name. – Muthuraja Ramalingam Aug 27 '18 at 08:50
  • I delete my answer then :) May you add your solution and then may you mark as solved? – Adem Catamak Aug 27 '18 at 09:31
  • No Adem, I am sticking full day with this. i tried your solution and then only i posted comment. var contentDisposition = "attachment; filename*=UTF-8''" + HttpUtility.UrlDecode(fileitem.filename); Response.Headers.Add("content-disposition", contentDisposition); – Muthuraja Ramalingam Aug 27 '18 at 09:40
  • Sorry, I misunderstood you :) – Adem Catamak Aug 27 '18 at 09:41
0

As per RFC 2045-

Current [RFC 2045] grammar restricts parameter values (and hence Content-Disposition filenames) to US-ASCII. We recognize the great desirability of allowing arbitrary character sets in filenames, but it is beyond the scope of this document to define the necessary mechanisms.

And you are doing right in escaping the same with UTF8. Having escaped characters in the downloaded file is expectedin this case.

For more information please check the below discussions-

How to encode the filename parameter of Content-Disposition header in HTTP?

and Here -

How to display a non-ascii filename in the file download box in browsers?

MBB
  • 1,635
  • 3
  • 9
  • 19