代码之家  ›  专栏  ›  技术社区  ›  Simant

下载文件是在带有IIS的Chrome浏览器中直接打开文件,而不是下载文件

  •  2
  • Simant  · 技术社区  · 6 年前

    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();
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Simant    6 年前

    我注意到这是Chrome的功能,要关闭它,我执行了以下步骤:

    1. 点击浏览器右上角的3个点进入设置
    2. 转到“下载”部分
    3. 单击“下载后自动打开某些文件类型”旁边的“清除”按钮