3

I am trying to build a functionality to download a csv file in C#.

When the name of the file has non-english character, the downloaded file does not seems to have the correct name. However in the network tab, the response header has the same Content-Disposition value, as given in the code.

Sample Code

private void PopulateCsvInResponse(MemoryStream csvData, string fileName)
{
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    //actual file name "Москва.csv"
    response.AddHeader("Content-Disposition", "attachment; filename=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");
    byte[] byteArray = csvData.ToArray();
    response.AddHeader("Content-Length", byteArray.Length.ToString());
    response.ContentType = "text/csv; charset=utf-8";
    response.BinaryWrite(byteArray);
    response.Flush();
    response.Close();
}

For example the file name is Москва.csv.
UTF-8 encoded name : %D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv.

Things that I tried

Replacing Content-Disposition header

Attempt 1

response.AddHeader("Content-Disposition", 
   "attachment; filename=Москва.csv");   

The downloaded file name is

Ð_оÑ_ква


Attempt 2

response.AddHeader("Content-Disposition", 
   "attachment; filename=\"%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv\"; filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");

The downloaded file name is

_%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv_; filename_


Attempt 3

response.AddHeader("Content-Disposition", 
    "attachment; filename=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");

The downloaded file name is

%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv


Attempt 4

response.AddHeader("Content-Disposition", 
    "attachment; filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv");

The downloaded file name is

UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv

  • 1
    i see your problem, you code starts with numbers, compiler error – TheGeneral Jun 23 '20 at 23:11
  • Those are line numbers @TheGeneral. They're not part of the code. If it were a compile error, the code wouldn't be running at all. – Robert Harvey Jun 23 '20 at 23:12
  • @RobertHarvey I was going to edit this question, but it seems like a lot of work – TheGeneral Jun 23 '20 at 23:14
  • How do you propose to edit it? The line numbers are for reference purposes; they have no negative effect on the question. – Robert Harvey Jun 23 '20 at 23:15
  • could you try only the UTF-8 part , in point 2 of the question : `attachment; filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.csv` – Pac0 Jun 23 '20 at 23:28
  • Does this answer your question? [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) – Gusman Jun 24 '20 at 00:01
  • @Pac0 this is the file name that I am getting "UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0". I have updated the question with this case as well. – Indrasen Singh Jun 24 '20 at 00:36
  • @Gusman I tried the approaches mentioned in the question, but none of the approaches is working for me. – Indrasen Singh Jun 24 '20 at 00:37
  • @TheGeneral Thanks alot for the edit. The question look a lot cleaner now!! – Indrasen Singh Jun 24 '20 at 00:39

1 Answers1

1

I finally found out the solution.
The issue was never with above code, it was always working fine. The actual issue was in front end, where the content disposition header that was received in encode was not decoded, and I skipped to see this part when I raised the question.
I thought of deleting this question, but keeping it so that if someone makes the same silly mistake as me, might realise it earlier instead of wasting time to look for solution for the problem that never existed.

  • hi, i am having the same issue how is the frontend related to this? Can you explain this furtherly? – tealy Oct 30 '22 at 13:56