Response.TransmitFile() fails to transmit an empty file

700 views Asked by At

I'm implementing a column in GridView to allow users to download files when they click on the file name(files are stored in Uploads folder of my Project).

Code is working fine when there's some data in the file i.e. user can click on file and it'll get downloaded but when a user clicks on a file which is empty (like an empty .docx file) a blank page is shown instead of downloading the file.

here's the code:

else if (e.CommandName == "Download")
        {
            Response.Clear();

            if (File.Exists(Server.MapPath("~/Uploads/") + e.CommandArgument))
            {               
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument.ToString());
                Response.TransmitFile(Server.MapPath("~/Uploads/") + e.CommandArgument);
                Response.End();
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "File Not Found";                
            }           
        }

I've tested on Chrome, Mozilla and IExplorer.

1

There are 1 answers

1
Jose David On

you need change this line:

Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument.ToString());

for this one:

Response.AppendHeader("Content-Disposition", filename=\"" + e.CommandArgument.ToString()) \"";