BT下载文件方法

314 阅读1分钟

获取下载文件地址路径

android、ios 文件地址下载地址不一样

var url = 'http://www.sse.com.cn/disclosure/listedinfo/announcement/c/new/2021-06-30/600771_20210630_1_ekvsRjDV.pdf';

// 获取文件路径
app.getAppDirectoryEntry(function(res){
    var savePath = '';
    if(window.devicePlatform=="android"){
        savePath=res.sdcard;
    }else if(window.devicePlatform=="iOS"){
        savePath=res.documents;
    }
    
    downloadFile(`${savePath}/Download/${filename}.pdf`,url)
});

下载文件

new FileTransfer API文档

function downloadFile(fileEntry, uri) {
    var fileTransfer = new FileTransfer();
    fileTransfer.download(
        uri, 
        fileEntry, 
        // 下载成功
        function (entry) {
            // 打开文件
            app.openFile(entry.fullPath)
        },
        // 下载失败
        function (error) {
            
        },
        false,
        {
            //headers: {
            //    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            //}
        }
    );

}