下载文件是直接在 Chrome 带 IIS 的浏览器中打开文件,而不是下载文件
Download file is opening the file directly in Chrome browser with IIS, Not downloading the file
文件下载在 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();
}
}
我注意到这是 Chrome 的功能,要关闭它,我执行了以下步骤:
- 点击浏览器右上角的三个点进入设置
- 单击页面底部的 "Advanced" 按钮。
- 转到 "Downloads" 部分
- 单击 "Open certain file types automatically after downloading"
旁边的 "Clear" 按钮
文件下载在 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();
}
}
我注意到这是 Chrome 的功能,要关闭它,我执行了以下步骤:
- 点击浏览器右上角的三个点进入设置
- 单击页面底部的 "Advanced" 按钮。
- 转到 "Downloads" 部分
- 单击 "Open certain file types automatically after downloading" 旁边的 "Clear" 按钮