File download problem: filename with spaces truncated in FF and replaced by underscore in IE
May 7, 2009 1 Comment
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:
