我创建了一个下载PDF文件的链接,点击链接。
这是我的.cshtml:
<h1>Hello Shuainan</h1>
<a href="/Account/PdfDownload">download</a>
<input type="button" value="Download" onClick="download('test.pdf')" />
<script>
function download(file) {
window.location = file;
}
</script>
,下载功能是:
public void PdfDownload(string path, string fileName)
{
var myPDF = System.IO.File.ReadAllBytes(@"C:\Program Files\wkhtmltopdf\bin\myPDF.pdf");
Response.ContentType = "Application/pdf";
Response.Headers.Add("Content-Disposition", "attachment; filename=testPDF.pdf");
Response.Body.Write(myPDF, 0, 2048);
}
但是下载完成后我无法加载PDF文件。
答案 0 :(得分:0)
创建将文件操作结果返回为
的Action方法public ActionResult PdfDownload()
{
string TempFilePath = @"C:\Program Files\wkhtmltopdf\bin\myPDF.pdf";
if (System.IO.File.Exists(TempFilePath))
{
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "testPDF.pdf",
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(TempFilePath, "application/pdf");
}
else
{
return null;
}
}
然后在视图中使用:
<h1>Hello Shuainan</h1>
<a href="/Account/PdfDownload">download</a>