文件下载插件的使用

首先安装插件android

cordova plugin add cordova-plugin-file-transferios

代码ide

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")
                            // }
                        }
                    );

                }
            }

增长监听事件post

 

因为android和ios手机的基础路径不一样,因此经过下面代码判断android和ios的机型,在肯定基础路径url

相关文章
相关标签/搜索