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?
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.
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?
Try quoting the file name and not encoding it like so
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(_Filename) + "\"");
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.
Related
I am using Webforms and I am providing a possibility to download files. However their names have white spaces and commas. What is the way to ensure that all file name will be there? eg if I have a name like:
1991-02-21 1111, ABCD, restofmyfilename.zip
all I get while dowloading is
1991-02-21
code part:
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment;filename=" + response.FileName);
Response.BinaryWrite(response.Bytes);
Response.End();
Did you try wrapping the file name in quotes? For Example:
Response.AddHeader("content-disposition", "attachment;filename=\"" + response.FileName + "\"");
You can slightly modify the FileName to avoid white spaces:
string fileName = response.FileName.Replace(' ','_');
then use this as a filename in your code.
I have to download file from server to browser. The code actually works, but it appends '_' characters to the beginning and end of the file.
Could you please help me to solve that? I have no idea why that strange stuff happens...
Here's the codes:
Response.AddHeader("Content-Disposition", "attachment; filename=\" + "p.jpg" + "\")
Response.ContentType = "application/octet-stream"
Response.WriteFile(Server.MapPath("~/App_data/PROJECTS_DATA/p.jpg"))
Use Below:
Response.ContentType = "image/jpg";
I have this problem in this piece of code:
resp.addHeader("Content-Disposition", "inline; filename=" + fileName);
When the file name is : a_b_c.doc or abc.doc , the name of the downloaded file is correct.
However, when the file name is : a b c.doc , the name of the downloaded file is only "a".
How can I work around this?
Many thanks!
Put quotes around the filename like this:
resp.addHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
Concatenate the file name after perform the url encode on the file name string.
The spec explains this, and gives explicit examples with spaces in the file name.
I want to download the files from server. When downloading i want to change the file name.
But I can't change the file name totally when the filename contains spaces.
My Code is as Below
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
This lets the user save the file to their computer.
But when i save the file, only the first word of the filename is saved.
eg. I want to give the file name as ("CCNA Q&A.pdf") but the file save as ("CCNA")
I want to know how to save the filename with spaces.
in firefox: Server.UrlEncode() will replace space with %20
just put the filename between double quotes filename = "\"" + filename + "\"";
Have you tried Server.UrlEncode()?
Or you can omit all non alphabetic characters (' ', -, etc) whith _-s (regular expression replace).
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
I was having the same problem as I was trying to download a text file having spaces in its name. I have solved this using UrlDecode function. As browser converts spaces to '+' symbol, using decode function one need to convert these '+' to spaces.
fileName = Server.UrlDecode(fileName);
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + "\"");
You'll have to wrap it in additional quotes:
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + "\"");
So, I user a bit of code to force a download on my ASP.Net based project. This bit of code works in Firefox and Chrome, bu not in IE for some strange reason. Even stranger, it worked in all three initially, and just stopped working in IE recently. Below is the code I used, please let me know if any adjustments need to be made or what the problem with with may be.
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "Application/pdf";
Response.AppendHeader("content-disposition","attachment; filename=" + path);
Response.WriteFile(path);
Response.End();
More details
Here is the revamped code, still doesnt work for IE.
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "Application/pdf";
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = type;
Response.AddHeader("content-disposition","attachment; filename=" + path);
Response.WriteFile(path);
Response.End();
You should probably try to set the mime type to "application/octet-stream". If you don't want a specific handler to respond to the mime-type.
Should this code
Response.AddHeader("content-disposition","attachment; filename=" + path);
be changed as
Response.AddHeader("content-disposition","attachment; filename=" + name + "." + ext);
or
Response.AddHeader("content-disposition","attachment; filename=" + name + ".pdf");
Other things to check for
Response.Buffer to true in the beginning
Response.clear in the beginning
Use response.binarywrite instead of writefile
Response flush at the end
Ensure no HTML or space characters written to the response.stream other than the binarywrite.
Problem solved. The reason it was not going through was due to an extra form that was present on the master page, apparently overlaying the buttons. Once that was fixed, It worked properly in all browsers.
Adding the following two lines at the top, fixed it for me:
Response.ClearContent();
Response.ClearHeaders();