我需要访问一个API端点,它返回一个pdf文件(不是pdf的url,而是实际的数据),然后以某种方式在我的ionic应用程序中显示这个pdf。理想情况下,我只想把它提供给其他应用程序,比如手机的移动web浏览器,但我也愿意尝试将它嵌入到我的应用程序中。在iOS上,我只使用$window.open(网址),而mobile safari知道如何下载和显示返回的pdf。然而,Android试图下载该文件,然后告诉我,当我试图打开它时,它无法打开。
我也尝试过用<embed>将其嵌入到应用程序中,但没有嵌入任何东西。但是,类似的方法也适用于<img ng-src="url">中的图像。
我也曾尝试过使用cordova FileOpener2,但我在这方面遇到了很多麻烦。如果这是做这件事的正确方式,我愿意重新访问这个方法。
我得到的最接近的方法肯定是直接把它发送到设备的移动浏览器上,因为它在iOS上运行得很好。
发布于 2015-12-29 04:42:40
我用filetransfer和fileopener2解决了它。我的代码如下。我遇到的主要问题是我的config.xml文件中没有<access origin="cdvfile://*" />,也没有正确安装ngCordova。
if (ionic.Platform.isIOS())
$window.open(APIUrl, '_blank', 'location=no');
else if (ionic.Platform.isAndroid()) {
var fileExtension = filename.substr(filename.lastIndexOf('.')+1);
//I have a dictionary with this somewhere else
var MIMEType = extToMime[fileExtension];
var uri = encodeURI(APIurl);
var fileURL = "cdvfile://localhost/persistent/file."+fileExtension;
$cordovaFileTransfer.download(uri, fileURL, {}, true)
.then(function(result) {
$cordovaFileOpener2.open(
fileURL,
MIMEType
).then(function() {
console.log("SUCCESS");
}, function(e) {
console.log("ERROR");
console.log(JSON.stringify(e));
});
}, function(e) {
console.log("Error: " + JSON.stringify(e));
});
}https://stackoverflow.com/questions/34497092
复制相似问题