我正在研究 angularjs 和 java 应用程序。下面的 html 代码用于在浏览器中打开 pdf 文件。问题是当data 属性的值被动态传递时,PDF 没有被打开,如下所示。
<div ng-controller="myPDFFileController">
{{pdfName}} <!-- /resources/myFiles/test1.pdf -->
<object data="{{pdfName}}" type="application/pdf" width="100%" height="100%">
<iframe src="{{pdfName}}" width="100%" height="100%" style="border: none;">
This browser does not support PDFs. Please download the PDF to view it:
</iframe>
</object>
js代码:
app.controller('myPDFFileController', function ($scope,MyService) {
$scope.getPDFPathFileName = function () {
MyService.getPDF().then(
function (response) {
$scope.pdfName = "/resources/myFiles/"+response;
},
function (errResponse) {
$rootScope.showError("Internal error" + errResponse);
});
}
$scope.getPDFPathFileName();
});
//service call
myService.getPDF = function(){
var Url = myURL+'/filesDataLink/getPDF.form';
$http.get(repUrl)
.then(
function (response) {
//return response
}
//return statement
}
java控制器:
@RequestMapping(value = "/getPDF", method = RequestMethod.GET,produces="text/plain")
public @ResponseBody String getPDF(HttpServletRequest request) throws Exception {
//code to creae a pdf file and return the filename
String filename = "test1";
File f = new File(filename);
//logic to generate pdf file with data
return filename;
}
}
在浏览器控制台中发现异常:
Failed to load resource: the server responded with a status of 404 () :8080/%7B%7BpdfName%7D%7D
请建议如何在运行时动态传递文件名并使 PDF 在浏览器上打开。当我在 html 代码本身中给出文件名而不是动态加载时,下面的代码有效。
将文件名分配给数据属性时的工作 html 代码:
<object data="/resources/myFiles/test1.pdf" type="application/pdf" width="100%" height="100%">
<iframe src="/resources/myFiles/test1.pdf" width="100%" height="100%" style="border: none;">
This browser does not support PDFs. Please download the PDF to view it:
</iframe>
</object>
--EDIT-- 现在我可以在 IE 和 chrome 的网页中看到 pdf 图像,但问题是 IE 在加载 pdf 之前显示警报。下面是加载页面时出现在 IE 中的警告框图像,关闭警告框后显示 pdf。控制台中未显示任何错误。