Asp.net mvc 2 如何在MVC2中强制下载?

Asp.net mvc 2 如何在MVC2中强制下载?,asp.net-mvc-2,Asp.net Mvc 2,我有一个pdf,想为用户提供一个简单的“下载”链接。 这是怎么做到的 我的想法是 -要计算服务器端pdf文档的url并将其存储在“viewmodel.PDFURL”中, -在调用函数的视图中添加一个。 -此函数将使用 $.post("ForcePDFDownload", { PDFURL: <%: Model.PDFURL %> } ); 但是返回null对我来说毫无意义,但是方法必须返回一些东西,否则Visual Studio将无法编译 有什么想法吗 提前谢谢 无需使用JSON、

我有一个pdf,想为用户提供一个简单的“下载”链接。 这是怎么做到的

我的想法是
-要计算服务器端pdf文档的url并将其存储在“viewmodel.PDFURL”中,
-在调用函数的视图中添加一个

-此函数将使用

$.post("ForcePDFDownload", { PDFURL: <%: Model.PDFURL %> } );
但是
返回null对我来说毫无意义,但是方法必须返回一些东西,否则Visual Studio将无法编译

有什么想法吗


提前谢谢

无需使用JSON、ajax、jquery或任何东西。简单地说:

public ActionResult ForcePDFDownload(string PDFURL)
{
    string path = Path.GetFullPath(PDFURL);
    string filename = Path.GetFileName(PDFURL);
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
    return File(path, "application/pdf");
}
然后构建一个链接:

<%: Html.ActionLink("Download PDF", "ForcePDFDownload", new { PDFURL = Model.PDFURL }) %>

快乐地度过余生:-)

哇!那很快,真的很好!非常感谢!作品
<%: Html.ActionLink("Download PDF", "ForcePDFDownload", new { PDFURL = Model.PDFURL }) %>
http://foo.com/somecontroller/forcepdfdownload/?pdfurl=c%3A%5Cmycreditcardnumbers.txt