0

Here is my code :

 Response.StatusCode = (int)HttpStatusCode.OK;
 Response.Headers.Add(HeaderNames.ContentDisposition, "attachment; filename=\"你好.zip\""); 

It occurs 500 error when download the zip, because the words of "你好".

If I using 'Hello.zip', it will work fine.


The error message is :

Invalid non-ASCII or control character in header: 0x4F60
LouraQ
  • 6,443
  • 2
  • 6
  • 16
  • What's the error that goes with the 500 response? And can I suggest using [`FileResult`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.fileresult?view=aspnetcore-5.0) rather than serving it manually? – ProgrammingLlama Jul 02 '21 at 10:05
  • 1
    This might be useful: [How to encode the filename parameter of Content-Disposition header in HTTP?](https://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http) – canton7 Jul 02 '21 at 10:08
  • Use `System.Web.HttpUtility.UrlEncode` to encode the file name – Fabjan Jul 02 '21 at 10:08

2 Answers2

-1

This seems an option if you know the Path

string fileName = "chinese.zip";
string newFileName = "Hello.zip";

string filePath = Server.MapPath(string.Format("~/Files/{0}", fileName));

Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; 
filename=" + newFileName);
  • Since i am also pretty new to programming please let me know why my answer is wrong so i can learn from it. ;) – Jurgen Volders Jul 02 '21 at 12:01
  • The whole point of the question is to deal with a file name when it's set to `"你好.zip"`, i.e. which has non-ASCII characters in it. There's nothing in your post that in any way addresses that question. It's not clear what question you were trying to answer, but it's pretty clear it's not the one on this page. :( – Peter Duniho Jul 05 '21 at 02:17
-3

You can use this instead of the chinese letters:

String.fromCharCode(20320)+String.fromCharCode(22909)

should looks like this:

Response.Headers.Add(HeaderNames.ContentDisposition, "attachment; filename="\" + String.fromCharCode(20320)+String.fromCharCode(22909) + ".zip\""); 

I think this should work

  • 4
    `String.fromCharCode` isn't a method in C#, and even if it was, there would be no difference between this and `"\uxxxx\uxxxx"`, which itself should be no different to putting the literal characters there. – canton7 Jul 02 '21 at 10:18