KaushaL.NET

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

Posted by: Kaushal on: May 7, 2009

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:

Read More

Leave a Reply

You must be logged in to post a comment.

Victims

  • 38,488 Hits

Calendar

May 2009
M T W T F S S
« Apr   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

Archives

KaushaL Parik (Microsoft MVP – ASP.NET)