tags:

views:

69

answers:

3

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?

+3  A: 

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
RFC-822 says that it needs to be double-quotes. (See the `quoted-string` definition.)
LukeH
@LukeH - Cheers - I'll update the answer
ChrisF
A: 

I found the solution :)

We have to surround the filename with double cotation like :

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

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

abdelrahman ELGAMAL
A: 

Try quoting the file name and not encoding it like so

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