在浏览器中使用ASP.NET MVC和C#显示或下载pdf
我读过很多关于这个的帖子,看起来很简单,但这让我发疯,我就是不明白为什么 我正在创建一个PDF文件,并试图显示它或下载它,但没有任何效果 这段代码显示了我的许多尝试,试图找出什么是错误的,但似乎没有什么是错误的 起初,我试图将PDF直接保存到流中,但由于它没有在浏览器中下载/显示,所以我将其保存到本地文件,并做了许多事情来确保一切正常在浏览器中使用ASP.NET MVC和C#显示或下载pdf,c#,asp.net-mvc,pdf,C#,Asp.net Mvc,Pdf,我读过很多关于这个的帖子,看起来很简单,但这让我发疯,我就是不明白为什么 我正在创建一个PDF文件,并试图显示它或下载它,但没有任何效果 这段代码显示了我的许多尝试,试图找出什么是错误的,但似乎没有什么是错误的 起初,我试图将PDF直接保存到流中,但由于它没有在浏览器中下载/显示,所以我将其保存到本地文件,并做了许多事情来确保一切正常 public ActionResult CreatePdfExport(string html) { string stylesheet = System
public ActionResult CreatePdfExport(string html)
{
string stylesheet = System.IO.File.ReadAllText(Server.MapPath(@"~/Content/print.css"));
PdfDocument pdf = CommonService.GetPdf(html, stylesheet);
var ms = new MemoryStream();
pdf.Save(ms, false); //save to stream
ms.Flush();
ms.Position = 0;
using (var file = System.IO.File.OpenWrite(Server.MapPath(Path.Combine("~/Content/justcreated.pdf"))))
ms.CopyTo(file); //save to file to check if stream is good, and it is
MemoryStream msfromfile = new MemoryStream();
using (FileStream filefromdisk = new FileStream(Server.MapPath(Path.Combine("~/Content/justcreated.pdf")), FileMode.Open, FileAccess.Read))
filefromdisk.CopyTo(msfromfile); //now get stream for file
msfromfile.Position = 0;
using (FileStream filetodisk = new FileStream(Server.MapPath(Path.Combine("~/Content/new.pdf")), FileMode.Create, System.IO.FileAccess.Write))
msfromfile.CopyTo(filetodisk); //save to new file from new stream, all good, both files are good
MemoryStream msfromfile2 = new MemoryStream();
using (FileStream filefromdisk2 = new FileStream(Server.MapPath(Path.Combine("~/Content/justcreated.pdf")), FileMode.Open, FileAccess.Read))
filefromdisk2.CopyTo(msfromfile2); //we now know the stream is good, we try to use it to respond
// msfromfile2.Flush(); //this flush doesn't really affect, I get same invalid file with or without
msfromfile2.Position = 0;
// this is an attempt to try to return bytes directly, still same invalid file
// return File(Server.MapPath(Path.Combine("~/Content/new.pdf")), "application/pdf");
// invalid file, even if i set headers like content-type, content-disposition, nothing works
return File(msfromfile2, "application/pdf", "pdf_downloaded.pdf");
}
我确实得到了类似PDF文件的响应数据,但它是无效的:
%PDF-1.4
%ÓôÌá
1 0 obj
<<
/CreationDate(D:20210407211253-06'00')
/Creator(PDFsharp 1.32.3057-g \(www.pdfsharp.net\))
/Producer(PDFsharp 1.32.3057-g \(www.pdfsharp.net\))
>>
endobj
2 0 obj
<<
/Type/Catalog
/Pages 3 0 R
>>
endobj
3 0 obj
<<
/Type/Pages
/Count 1
/Kids[4 0 R]
>>
endobj
4 0 obj
<<
etc
etc
etc
如果我尝试使用JavaScript打开,从数据响应创建blob,如下所示:
$scope.pdf = function () {
var str_div = document.getElementById('information').innerHTML;
growerReports.createPdf(str_div).then(function (response) {
var file = new File([response.data], "mipdf.pdf", { type: "application/pdf" });
var fileURL = URL.createObjectURL(file);
$window.open(fileURL); //blank pdf
$window.open("http://localhost:19260/Vendors/Content/justcreated.pdf"); //the pdf i want
});
}
当我打开blob url时,我得到一个空白的pdf,这可能是因为pdf无效。文件的直接url将正确打开
所有这些都指向文件函数并没有返回我需要的内容,但这太奇怪了
return File(msfromfile2, "application/pdf", "pdf_downloaded.pdf")
因此,我尝试将ActionResult更改为FileContentResult
和FileStreamResult
,但它们都不起作用
我已经尝试了很多东西,所以我不知道还能尝试什么
也许我遗漏了一些非常简单的东西
return File(msfromfile2, "application/pdf", "pdf_downloaded.pdf")