File download problem: filename with spaces truncated in FF and replaced by underscore in IE
This
is a mere note to remember for future rather than a blog entry. While I am working
on code to download file from server. I write below code:
{
int ChunkSize = 10000;
string sFileFullPath = Server.MapPath("New Text Document.txt");
System.IO.FileInfo toDownload = new System.IO.FileInfo(sFileFullPath);
if (System.IO.File.Exists(sFileFullPath))
{
Response.Clear();
using (FileStream iStream = System.IO.File.OpenRead(sFileFullPath))
{
long dataLengthToRead = iStream.Length;
Byte[] buffer = new Byte[dataLengthToRead];
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
while (ChunkSize > 0 && Response.IsClientConnected)
{
if (ChunkSize > dataLengthToRead)
{
ChunkSize = int.Parse(dataLengthToRead.ToString());
}
int lengthRead = iStream.Read(buffer, 0, ChunkSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
}
Response.Close();
Response.End();
}
else
{
this.Page.ClientScript.RegisterStartupScript(GetType(), "ShowMessage", "<script language='javascript'>alert('No Files Available');</script>");
}
}
When I ran the application I found that the file which I
need to download was containing spaces in its name like “New Text Document.txt”.
The problem is:
In FF, was truncating the filename from space while showing
the file download dialog . So, its only showing the filename as “New”
(and truncating the rest of the filename after space).

In IE, was replacing the spaces in filename with underscore
( _ ) as “New_Text_Document.txt” while showing the file download dialog
box.

This something I dont want to have. I need to have the exact
filename as it is on server.
The solution is:
For FF: you need to enclose the name of the file in Quotes
while you add it as attachment in response header as:
Response.AddHeader("Content-Disposition",
"attachment;
filename=\""
+ filename +
"\"");
For IE: you need to identify and replace the space in
filename with “%20” (something like encoding filename) as:
String userAgent = Request.Headers.Get("User-Agent");
String filename = toDownload.Name;
if (userAgent.Contains("MSIE 7.0"))
filename = toDownload.Name.Replace(" ", "%20");
Thats It! Hope it would be helpful for
someone in same need!
Complete Code would be:
{
int ChunkSize = 10000;
string sFileFullPath = Server.MapPath("New Text Document.txt");
System.IO.FileInfo toDownload =
new System.IO.FileInfo(sFileFullPath);
if (System.IO.File.Exists(sFileFullPath))
{
Response.Clear();
using (FileStream iStream = System.IO.File.OpenRead(sFileFullPath))
{
long dataLengthToRead = iStream.Length;
Byte[] buffer = new Byte[dataLengthToRead];
Response.ContentType = "application/octet-stream";
String userAgent = Request.Headers.Get("User-Agent");
String filename = toDownload.Name;
if (userAgent.Contains("MSIE 7.0"))
filename = toDownload.Name.Replace(" ", "%20");
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
while (ChunkSize > 0 && Response.IsClientConnected)
{
if (ChunkSize > dataLengthToRead)
{
ChunkSize = int.Parse(dataLengthToRead.ToString());
}
int lengthRead = iStream.Read(buffer, 0, ChunkSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
}
Response.Close();
Response.End();
}
else
{
this.Page.ClientScript.RegisterStartupScript(GetType(), "ShowMessage", "<script language='javascript'>alert('No Files Available');</script>");
}
}