Alex Rivera | Logout

File download problem: filename with spaces truncated!

Asked 2010-09-17T15:08:41.177
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?

Edit
Report

1 Answer

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.

answered 2010-09-17T15:11:13.010

Your Answer