17

While I am working on code to download file from server using :

Response.AddHeader("Content-Disposition", "attachment; filename=" + 
Server.UrlPathEncode(Path.GetFileName(_Filename)));

The problem is while having spaces in the file name, with this code the server split automatically while finding the first space!

I'm hoping to know Why & what is the solution for that?

Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
Abdelrahman ELGAMAL
  • 414
  • 2
  • 7
  • 15

4 Answers4

42

You need to wrap the filename in double quotes.

string filename = Server.UrlPathEncode(Path.GetFileName(_Filename)));
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

Otherwise the code assumes that the filename ends at the first space.

You might not need the Server.UrlPathEncode.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
6

I found the solution :)

We have to surround the filename with double quotation mark like :

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(_Filename) + "\"");

But up till now, I didn't have any idea for this split?

Nazmul
  • 7,078
  • 12
  • 51
  • 63
Abdelrahman ELGAMAL
  • 414
  • 2
  • 7
  • 15
2

Try quoting the file name and not encoding it like so

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(_Filename) + "\"");
amarsuperstar
  • 1,783
  • 1
  • 17
  • 22
0

This is the case with firefox..

I found an answer by Alfonso Martinez here: https://bugzilla.mozilla.org/show_bug.cgi?id=221028#c1

[Alfonso Martinez] was talking about this issue in #mozillazine with Christian Biesinger and Boris Zbarsky, and they said that this is the proper behaviour according to the RFC.

The solution it's just to put the quoted filename and then everything will work fine as that is the expected syntax.

jflaga
  • 4,610
  • 2
  • 24
  • 20