文件下载插件的使用

首先安装插件

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的机型,在确定基础路径

 

posted @ 2018-11-22 11:34  codeMing  阅读(503)  评论(0编辑  收藏  举报