【问题标题】:Download file is opening the file directly in Chrome browser with IIS, Not downloading the file下载文件是用 IIS 直接在 Chrome 浏览器中打开文件,而不是下载文件
【发布时间】:2019-01-20 02:21:41
【问题描述】:

文件下载在 Microsoft Edge、Firefox 和 Safari 浏览器中运行良好,但在 Chrome 中不是下载文件,而是直接打开文件。我还想补充一点,只有当应用程序托管在 IIS 中并且在 IISExpress 中所有浏览器都可以正常工作时,才会出现这种行为。为什么我在带有 Chrome 的 IIS 中出现这种行为以及如何解决该问题?

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();
    }
}

【问题讨论】:

  • 这是客户端电脑的浏览器/计算机行为。不是你可以用代码解决的问题。

标签: asp.net iis webforms iis-express


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-03-08
    • 2011-11-08
    • 2016-12-14
    • 2011-10-18
    • 2016-09-11
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多