场景如下:我在InAppBrowser中打开一个网站,在用户完成工作后,该网站会生成一个.pdf供用户下载,问题是pdf文件不会下载,而是在浏览器中打开。
有办法让它从InAppBrowser中下载吗?我现在正在开发一个iOS应用程序,所以最好提供适用于iOS的解决方案。
先行致谢。
有办法让它从InAppBrowser中下载吗?我现在正在开发一个iOS应用程序,所以最好提供适用于iOS的解决方案。
先行致谢。
遵循@jcesarmobile的建议,这是我想到的:
首先,我需要安装cordova-plugin-file-transfer
var url = "http://mi-fancy-url.com";
var windowref = window.open(url, '_blank', 'location=no,closebuttoncaption=Cerrar,toolbar=yes,enableViewportScale=yes');
windowref
上创建一个监听器,以检查正在加载的内容是否为pdf文件(这是我的情况),监听loadstart
事件。windowref.addEventListener('loadstart', function(e) {
var url = e.url;
var extension = url.substr(url.length - 4);
if (extension == '.pdf') {
var targetPath = cordova.file.documentsDirectory + "receipt.pdf";
var options = {};
var args = {
url: url,
targetPath: targetPath,
options: options
};
windowref.close(); // close window or you get exception
document.addEventListener('deviceready', function () {
setTimeout(function() {
downloadReceipt(args); // call the function which will download the file 1s after the window is closed, just in case..
}, 1000);
});
}
});
function downloadReceipt(args) {
var fileTransfer = new FileTransfer();
var uri = encodeURI(args.url);
fileTransfer.download(
uri, // file's uri
args.targetPath, // where will be saved
function(entry) {
console.log("download complete: " + entry.toURL());
window.open(entry.toURL(), '_blank', 'location=no,closebuttoncaption=Cerrar,toolbar=yes,enableViewportScale=yes');
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
true,
args.options
);
}
我现在面临的问题是下载路径,我只是无法打开它。但好在文件已经下载。我将不得不创建一个localStorage项目来保存不同文件的路径。
在这些步骤中缺少许多验证,这只是我快速制作的示例,以检查其是否有效。需要进一步验证。
setTimeout
是普通的 JavaScript。 - João Pimentel Ferreira使用IAB插件打开窗口并添加事件监听器 ref = window.open(url, "_blank"); ref.addEventListener('loadstop', loadStopCallBack);
在InAppBrowser窗口中使用https://xxx.pdf">documentName调用操作
实现loadStopCallBack函数
function loadStopCallBack(refTemp) {
if(refTemp.url.includes('downloadDoc')) {
rtaParam = getURLParams('downloadDoc', refTemp.url);
if(rtaParam != null)
downloadFileFromServer(rtaParam);
return;
}
}
function getURLParams( name, url ) {
try {
if (!url)
url = location.href;
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(url);
return results == null ? null : results[1];
} catch (e) {
showSMS(e);
return null;
}
}
function downloadFileFromServer(fileServerURL){
try {
var Downloader = window.plugins.Downloader;
var fileName = fileServerURL.substring(fileServerURL.lastIndexOf("/") + 1);
var downloadSuccessCallback = function(result) {
console.log(result.path);
};
var downloadErrorCallback = function(error) {
// error: string
console.log(error);
};
//TODO cordova.file.documentsDirectory for iOS
var options = {
title: 'Descarga de '+ fileName, // Download Notification Title
url: fileServerURL, // File Url
path: fileName, // The File Name with extension
description: 'La descarga del archivo esta lista', // Download description Notification String
visible: true, // This download is visible and shows in the notifications while in progress and after completion.
folder: "Download" // Folder to save the downloaded file, if not exist it will be created
};
Downloader.download(options, downloadSuccessCallback, downloadErrorCallback);
} catch (e) {
console.log(e);
}
}
你可以在这里获取插件 https://github.com/ogarzonm85/cordova-plugin-downloader
它很好用也很容易