【发布时间】: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