【发布时间】:2013-08-28 23:29:27
【问题描述】:
这是我的代码,我尝试按照以下方式放置下载文件的功能,但它无法正常工作。它不显示保存文件对话框。
protected virtual FileResult Download(string FileName, string FilePath)
{
Response.AppendHeader("Content-Length", FileName.Length.ToString());
return File(FilePath, "application/exe", FileName);
}
也尝试过这种方式:
protected virtual ActionResult Download(string FileName, string FilePath)
{
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.AppendHeader("Content-Length", FileName.Length.ToString());
Response.ContentType = "application//x-unknown";
Response.WriteFile(FilePath.Replace("\\", "/"));
Response.Flush();
Response.End();
}
但两者都不起作用。我错过了什么?
【问题讨论】:
-
我希望您了解文件路径和文件名变量伴随的安全问题...
-
文件名长度!= 内容长度。此外,它可能的浏览器正在阻止可执行文件下载。我知道我的 IE 确实如此...... Chrome 会抛出一个“你绝对确定吗?”提示。
-
看到这个类似的问题:stackoverflow.com/questions/3604562/…
-
我也尝试使用 .doc 文件,并且在同一位置它适用于 asp.net 中的 exe 文件。
-
这可能是因为文件压缩(我相信默认)。文本有效,因为浏览器知道如何解压缩文件。压缩后的二进制文件(PDF、DOCX 等)看起来仍然像二进制文件。
标签: c# asp.net-mvc-4 download mime-types