场景是这样的:我在InAppBrowser中打开一个网站,在用户结束工作之后,该网站生成一个.pdf供用户下载,问题是pdf没有下载,它打开它在浏览器中。
有没有办法让它从InAppBrowser下载?我目前正在开发iOS应用程序,因此iOS的解决方案会更好。
提前致谢。
答案 0 :(得分:8)
关注@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
上为loadstart
事件创建一个侦听器,并检查所加载的内容是否为pdf(这是我的情况)。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 () {
$timeout(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项来保存不同文件的路径。
此步骤中缺少许多验证,这只是我快速检查其是否有效的示例。需要进一步验证。
答案 1 :(得分:0)
使用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
处获得该插件它有效并且太简单了