4

这是我的代码,我尝试了以下方式来放置下载文件的功能,但它不能正常工作。它不显示保存文件对话框。

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

但两者都不起作用。我错过了什么?

4

2 回答 2

4

我不知道主要区别,但是对于任何文件,以下代码对我来说都可以正常工作。可能是因为 @Garath 建议的 ContentType 。

var fileInfo = new System.IO.FileInfo(oFullPath);
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", yourfilename));
            Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            Response.WriteFile(oFullPath);
            Response.End();
于 2014-04-14T05:32:05.080 回答
2

exe 文件的正确 mimitypeapplication/octet-stream不是application/exeapplication//x-unknown-检查 MSDN 您可以在此处查看更多定义:Get MIME type from filename extension

于 2013-08-26T13:07:55.397 回答