【发布时间】:2014-09-01 22:35:00
【问题描述】:
我有一个 MVC 应用程序。我想下载一个pdf。
这是我观点的一部分:
<p>
<span class="label">Information:</span>
@using (Html.BeginForm("DownloadFile")) { <input type="submit" value="Download"/> }
</p>
这是我的控制器的一部分:
private string FDir_AppData = "~/App_Data/";
public ActionResult DownloadFile()
{
var sDocument = Server.MapPath(FDir_AppData + "MyFile.pdf");
if (!sDocument.StartsWith(FDir_AppData))
{
// Ensure that we are serving file only inside the App_Data folder
// and block requests outside like "../web.config"
throw new HttpException(403, "Forbidden");
}
if (!System.IO.File.Exists(sDocument))
{
return HttpNotFound();
}
return File(sDocument, "application/pdf", Server.UrlEncode(sDocument));
}
如何下载特定文件?
【问题讨论】:
-
有什么问题?
标签: c# asp.net-mvc-4 razor