|
楼主 |
发表于 2017-5-9 15:03:33
|
显示全部楼层
if (Browser.isX5App) {
if (Browser.isAndroid) {
filePath = cordova.file.externalRootDirectory + '/easywits/attr/'; // 下载路径
}
if (Browser.isIOS) {
filePath = cordova.file.documentsDirectory + '/easywits/attr/'; // 下载路径
}
}
var uri = encodeURI(attr); // 下载地址
var open = cordova.plugins.disusered.open;
var isOpen = true;
window.resolveLocalFileSystemURL(filePath, function(fs) {
console.log('打开的文件系统: ' + fs.name);
var url = uri;
fs.getFile(name, {
create : true,
exclusive : false
}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
try {
fileWriter.seek(fileWriter.length);
} catch (e) {
console.log("file doesn't exist!");
}
if (fileWriter.length == 0) {
download(fileEntry, url);
} else {
if (isOpen) {
function success() {
}
function error(code) {
if (code === 1) {
window.plugins.toast.show("未知错误,请联系管理员!", "short", "bottom");
} else {
window.plugins.toast.show("未知错误,请联系管理员!", "short", "bottom");
}
}
// alert("打开");
// cordova.plugins.fileOpener2.open(fileEntry.nativeURL,
// "image/vnd.dece.graphic");
setTimeout(function() {
open(fileEntry.nativeURL, null, null);
}, 500);
}
}
});
}, onErrorCreateFile);
}, onErrorLoadFs);
// 下载文件
function download(fileEntry, uri) {
var fileTransfer = new FileTransfer();
var fileURL = fileEntry.toURL();
window.plugins.spinnerDialog.show("文档下载中", null, function() {
isOpen = false;
window.plugins.spinnerDialog.hide();
fileTransfer.abort();
});
fileTransfer.download(uri, fileURL, function(entry) {
window.plugins.spinnerDialog.hide();
if (isOpen) {
setTimeout(function() {
open(entry.toURL(), null, null);
}, 500);
// cordova.plugins.fileOpener2.open.open(entry.toURL(),
// null, null);
}
console.log("下载成功!");
console.log("文件保存位置: " + entry.toURL());
window.plugins.spinnerDialog.hide();
}, function(error) {
window.plugins.spinnerDialog.hide();
console.log("下载失败!");
console.log("error source " + error.source);
console.log("error target " + error.target);
console.log("error code" + error.code);
window.plugins.spinnerDialog.hide();
}, null, // or, pass false
{
// headers: {
// "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
// }
});
}
// 文件创建失败回调
function onErrorCreateFile(error) {
console.log("文件创建失败!")
}
// FileSystem加载失败回调
function onErrorLoadFs(error) {
console.log("文件系统加载失败!")
}
大神看下 |
|