Search code examples
asp.netiiswebformsiis-express

Download file is opening the file directly in Chrome browser with IIS, Not downloading the file


File download is working fine in Microsoft Edge, Firefox and Safari browsers but in Chrome instead of downloading a file, it's opening the file directly. I would also want to add that this behaviour is only when the application is hosted in IIS and in IISExpress all browsers work fine. Why am I getting this behaviour in IIS with Chrome and how to resolve the issue?

protected void btnDownloadFile_Click(object sender, EventArgs e)
{
    string fileFullPath = @"E:\AllFiles\History\John's certificate.doc";
    FileInfo fileInfo = new FileInfo(fileFullPath);
    if (fileInfo.Exists)
    {
        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();

        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileInfo.Name + "\"");
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());

        Response.ContentType = MIMETypeHelper.GetMimeType(fileInfo.Extension); // This method supplies the contenttype based on file extention for example if .doc extension returns application/msword

        Response.Flush();
        Response.TransmitFile(fileInfo.FullName);
        Response.End();
    }
}

Solution

  • I noticed that this is Chrome's feature and to turn it off I did the following steps:

    1. Go to setting by clicking on 3 dots on the right corner of the browser
    2. Click on the "Advanced" button at the bottom of the page.
    3. Go to the "Downloads" section
    4. Click on the "Clear" button beside "Open certain file types automatically after downloading"