1. 程式人生 > >檔案下載外掛的使用

檔案下載外掛的使用

首先安裝外掛

cordova plugin add cordova-plugin-file-transfer

程式碼

function downLoad(url) {
                document.addEventListener("deviceready", onDeviceReady, false);
                function onDeviceReady() {
                    hmsPopup.showLoading('loading');
                    var fileTransfer = new
FileTransfer(); var fileURL; if(navigator.platform == 'iPhone'){ fileURL = cordova.file.documentsDirectory+"1535075145153.xlsx"; }else{ fileURL = cordova.file.externalRootDirectory+"1535075145153.xlsx"; } fileTransfer.download( url, fileURL,
function(entry) { hmsPopup.hideLoading(); vm.localUrl = entry.toURL(); // alert("本地路徑"+vm.localUrl); // alert("localUrl"+vm.localUrl); dict.params.attachmentPath = vm.localUrl;
// alert("dict.params.attachmentPath"+dict.params.attachmentPath); HandBridge.postMessage(JSON.stringify(dict)); }, function(error) { hmsPopup.hideLoading(); console.log("download error source " + error.source); console.log("download error target " + error.target); console.log("download error code" + error.code); }, false, { // headers: { // "Authorization": 'Bearer' + " " + StorageService.getStorage("access_token") // } } ); } }

增加監聽事件

 

由於android和ios手機的基礎路徑不同,所以通過下面程式碼判斷android和ios的機型,在確定基礎路徑