用法:
var attachments = res.attachments[0]; //拿到附件的一些参数
var download = function(attachments) { //定义的一个下载附件方法
if (attachments && !attachments.attachmentUrl) {
return;
}
console.log('window.savePath',window.savePath)
var docuementName = attachments.attachmentName;
var filePath = window.savePath + "/" + docuementName;
console.log('下载处的filepath',filePath)
var url = attachments.attachmentUrl;
var fileTransfer = new FileTransfer();
fileTransfer.download(
url,
filePath,
function(entry) {
//将文件路径保存起来,方便后面调用
window.fileUri = entry.fullPath.replace("file:///", "file://");
var URL = window.fileUri;
app.openFile(URL, "", function(res) {
app.hint("打开成功!");
});
},
function(error) {
console.log('打开失败')
app.hint("下载失败!");
}
);
};
app.getAppDirectoryEntry(function (res) { //获取手机保存的文件路径
if (window.devicePlatform == "android") {
window.savePath = res.sdcard;
} else if (window.devicePlatform == "iOS") {
window.savePath = res.documents;
}
download(attachments); //调用方法
});
下载后打开文件方法openfile, 调用 bingotouch.js里面的方法
/**
* 打开文件:如office文件,apk等,将调用系统的程序来打开文件
* @method app.openFile
* @param filePath {String} 文件地址
* @param mime {String} mime类型
* @param success {Function} 打开成功回调
* @param fail {Function} 打开失败回调
* @example
* app.openFile("file:///mnt/sdcard/bingotouch.docx","docx",function(res){
* app.hint("打开成功!");
* });
*/
app.openFile = function(filePath, mime, success, fail) {
filePath = filePath || "";
mime = mime || "";
success = success || function(result) {};
fail = fail || function(result) {
app.hint("没有找到合适的程序打开该文件");
};
if (window.devicePlatform == "iOS") {
//ios无需带上file
filePath = filePath.replace("file://","");
}
Cordova.exec(success, fail, "ExtendApp", "openFile",[ filePath, mime ]);
}