【发布时间】:2015-05-26 13:00:56
【问题描述】:
我有这个Web API post 方法来生成PDF:
[HttpPost]
[Route("api/pdf")]
public HttpResponseMessage Post(CustomType type)
{
StreamContent pdfContent = PdfGenerator.GeneratePdf();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = pdfContent;
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "PDFName.pdf"
return response;
}
在 AngularJS 中,我想得到这样的 PDF 文件:
$http.post("api/pdf", object).then(function (results) {
var data = results.data;
//TODO: got PDF, how to handle?
}, function (results) {
console.log(results);
});
但是我应该如何处理 AngularJS 中的 PDF 数据?在 iPad 上,我希望 PDF 被打开,但如果文件是下载的或只是在桌面上打开并不重要。
EDIT1:
使用SaveAS JS library 我可以在桌面上制作一个可行的解决方案:
$http.post("api/pdf", object).then(function (results) {
var data = results.data;
var file = new Blob([data], { type: 'application/pdf' });
saveAs(file, "test.pdf");
}, function (results) {
console.log(results);
});
文件正在直接下载到我的桌面,但由于某种原因,此解决方案在 iOS 中不起作用。什么都没发生。看到这个JSFiddle。
有没有其他方法可以在我的 iPad 上显示 PDF 而无需在我的网站上嵌入文件?
【问题讨论】:
-
Dennis,您应该查看@DavidTansey 上面建议的帖子,它告诉您如何使用 arraybuffer、$sce 和 embed 在您的 angularjs 应用程序中获取和显示 pdf 文件
-
请看我的
EDIT1。 -
这个问题有几个方法可以试试stackoverflow.com/questions/26069378/…
-
我正在做一个 GET 并且必须通过
{ responseType: 'blob' }来让 AngularJS 停止损坏我的 PDF。 Gleaned from here$http.get("api/pdf", { responseType: 'blob' })