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ファイルの正しいmimitypeはそうではないapplication/octet-streamapplication/exeMSDN を確認してください。詳細については、ここを参照してください。ファイル名拡張子からMIMEタイプを取得するapplication//x-unknown

于 2013-08-26T13:07:55.397 に答える