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>");
}
}

Comments

# re: File download problem: filename with spaces truncated in FF and replaced by underscore in IE

Friday, September 04, 2009 7:01 AM by Priyanka

Thanks ......Helped me  a lot

# Dropbox plugin for wordpress | Software

Tuesday, September 29, 2009 11:49 AM by Dropbox plugin for wordpress | Software

Pingback from  Dropbox plugin for wordpress | Software

# re: File download problem: filename with spaces truncated in FF and replaced by underscore in IE

Wednesday, October 21, 2009 9:38 AM by macafee

Whatever you say, but I do not agree with your point of view about this issue

# re: File download problem: filename with spaces truncated in FF and replaced by underscore in IE

Saturday, October 31, 2009 8:32 AM by facebok

Your blog is very interesting. And how do you feel about social networks?

# re: File download problem: filename with spaces truncated in FF and replaced by underscore in IE

Friday, December 18, 2009 6:16 AM by tramodol

Always thought that your blog is one of the best in my bookmarks, and once again saw this