サーバーからファイルをダウンロードするためのチュートリアルに従っています。でも、困ったことがあります。私はいくつかのばかげた間違いをしている必要があります.. !!
これは私がフォローしているリンクです: http://haacked.com/archive/2008/05/10/writing-a-custom-file-download-action-result-for-asp.net-mvc.aspx
要件は次のとおりです。ユーザーがリンクをクリックすると、サイトは詳細ページに移動します。その詳細ページに、ダウンロード リンクがあります。ユーザーがそのダウンロード リンクをクリックすると、ファイルがダウンロードされます。
問題は、ダウンロード リンクをクリックしても、元のファイルがダウンロードされないことです。代わりに、HTML ファイルをダウンロードします。HTML ファイルをクリックすると、ゴミが表示されます。
これは私のコードです:
アクション:
public ActionResult Download(string path,string name)
{
return new DownloadResult { VirtualPath = path, FileDownloadName = name };
}
DownloadResult クラス
public class DownloadResult : ActionResult
{
public DownloadResult()
{
}
public DownloadResult(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public string VirtualPath
{
get;
set;
}
public string FileDownloadName
{
get;
set;
}
public override void ExecuteResult(ControllerContext context)
{
if (!String.IsNullOrEmpty(FileDownloadName))
{
context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + this.FileDownloadName);
}
string filePath = this.VirtualPath; //context.HttpContext.Server.MapPath(this.VirtualPath);
context.HttpContext.Response.TransmitFile(filePath);
}
}
チュートリアルと私のコードとの唯一の違いは、実際のサーバー パスを使用していることです。
何か案が??