【发布时间】:2018-02-12 09:02:07
【问题描述】:
我正在尝试使用命令从网络应用程序直接下载 pdf
window.open(target, '_blank', 'location=no');
在 Android 中它运行良好,但在 iOS 中它只显示 PDF 的预览,只有一个“完成”按钮,没有任何“下载”或“打开方式”操作按钮。
有什么办法可以解决吗?我读过一些东西,但没有找到任何东西..
谢谢
【问题讨论】:
我正在尝试使用命令从网络应用程序直接下载 pdf
window.open(target, '_blank', 'location=no');
在 Android 中它运行良好,但在 iOS 中它只显示 PDF 的预览,只有一个“完成”按钮,没有任何“下载”或“打开方式”操作按钮。
有什么办法可以解决吗?我读过一些东西,但没有找到任何东西..
谢谢
【问题讨论】:
iOS 的工作方式与 Android 不同,它无法下载文件。
您的选择是:
在 Safari 中打开,这样它就会显示出来,并且会有一个“打开方式”选项,允许您在另一个应用程序中打开它。
要在 Safari 中打开,请使用此代码 window.open(target, '_system');
在您的应用内下载。
在这里你必须选择,使用 XHR 呼叫或使用cordova-plugin-file-transfer
对于 XHR,请参阅我关于此答案的示例
Download binary data into the app sandbox using XHR2 request instead of cordova-file-transfer
有关文件传输,请参阅此示例代码:
// !! Assumes variable fileURL contains a valid URL to a path on the device,
// for example, cdvfile://localhost/persistent/path/to/downloads/
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/yourFile.pdf");
fileTransfer.download(
uri,
fileURL,
function(entry) {
console.log("download complete: " + entry.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("download error code" + error.code);
},
false,
{}
);
【讨论】: