如何在angularjs中阅读pdf流
我从服务器获得以下PDF流:
如何在AngularJS中读取此流? 我尝试使用以下代码在新窗口中将其作为PDF文件打开:
.success(function(data) { window.open("data:application/pdf," + escape(data)); });
但我无法在打开的窗口中看到内容。
我通过更改控制器代码实现了这一点
$http.get('/retrievePDFFiles', {responseType: 'arraybuffer'}) .success(function (data) { var file = new Blob([data], {type: 'application/pdf'}); var fileURL = URL.createObjectURL(file); window.open(fileURL); });
请看以下代码:
在控制器端 –
$http.get(baseUrl + apiUrl, { responseType: 'arraybuffer' }) .success(function (response) { var file = new Blob([response], { type: 'application/pdf' }); var fileURL = URL.createObjectURL(file); $scope.pdfContent = $sce.trustAsResourceUrl(fileURL); }) .error(function () { });
在HTML方面:
您也可以使用Angular ng-pdfviewer并使用它的js文件查看您的pdf。
看看PDF.JS。 这是一个客户端javascript库,可以获取pdf流并将其呈现给客户端。 Angular无法读取pdf因此这不是一个有角度的问题。
Java:获取方法
BLOB pdfData = getBlob_Data; response.setContentType(pdfData.getContentType()); response.setHeader(ApplicationLiterals.HEADER_KEY_CONTENT, "attachment; filename=FileName.pdf"); response.getOutputStream().write(pdfData.getBinaryData()); response.getOutputStream().flush();
使用config.responseType的问题是$ http服务仍然运行默认的responseTransformer并尝试将响应转换为JSON。 此外,您将发送默认接受标头。 这是一个(未经测试的)替代方案:
$http.get('/retrievePDFFiles', { headers: { Accept: "application/pdf"}, transformResponse: function(data) { return new Blob([data], {type: 'application/pdf'}); }}).success(function (data) { var fileURL = URL.createObjectURL(data); window.open(fileURL); });
- 如何将Cross Origin资源共享与Spring MVC 4.0.0 RESTful Webservice集成
- 在预检响应中,Access-Control-Allow-Methods不允许使用方法DELETE
- AngularJS中的文件上载JSON Spring MVC应用程序400错误请求所需的请求部分不存在
- Spring MVC – AngularJS – 文件上传 – org.apache.commons.fileupload.FileUploadException
- 如何组合现有的Google App引擎后端和AngularJS Web应用程序?
- 将jhipster后端和前端分成两个项目?
- 如何使用AngularJS $ http发送multipart / form-data
- 异步执行多个任务并在JavaScript函数中返回第一个成功的结果
- 为SPA前端配置Spring Boot