HttpResponse downloading zip file cuts zip file name - asp.net

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.

Related

Use of ("Content-Disposition", "inline; filename=" + fileName)

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.

Opening a word file after downloading from code behind(asp.net)

I have written code to open a Word document after downloading it in code behind. The document is opening fine, but it is not saving in Word format. When I am going to open it, it is asking for selecting the format to open the file.
The code is below:
string FullFilePath = "D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition", "inline; filename=\"" + txtDate.Text + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}
Set your content type to application/msword.
Refer: Microsoft Office MIME Types
You are not specifying an extension when sending the file name.
If your file saves without an extension, you will get the prompt asking for the application to use to open it.
Also, use the "Content-Disposition", "Attachment" if you want to tell the browser to save the file. inline will make the browser attempt to open the file in Word directly.
string FullFilePath =//path of file //"D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "Attachment; filename=\"" + txtDate.Text + ".doc\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}

ASP.NET - Download File When File Name Contain Spaces

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 + "\"");

File download problem: filename with spaces truncated!

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.

Save string to client with Open/Save dialog

I am using the following code to write the contents of a string (converted to a byte array) to the client in ASP.NET/C#
byte[] data = StrToByteArray(strData);
Response.ClearContent();
Response.AppendHeader("content-length", data.Length.ToString());
Response.ContentType = "text/plain";
Response.AppendHeader("content-Disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(data);
Response.Flush();
fileName is the name of the file ending with the file extension (.pgn). However, the file is saved as a .txt file, ignoring the extension that I am giving it. Would this have to do with the Response.Contenttype = "text/plain"? How can I get the Open/Save dialog to display and save the correct (.pgn) filename?
Also, if filename is a string separated by dashes or spaces, when the Open/Save dialog comes up, the filename is not displayed in its entirety but it is truncated where the first dash (-) or space (or comma) is encountered. How can this be remedied?
Yes, it is saving .txt because of your Content Type (MIME type). Use image/png.
How about you remove the dashes and spaces? String.Replace is great. fileName.Replace("-", ""); etc.

Resources