uniapp文件下载blob问题,uniapp使用webview引入另一个uniapp的h5时h5中的文件下载后blob,无法打开blob文件

1.window.location.href=blob时无法打开或者你直接写成window.location.href=‘http://xxxxx/xxx.doc’

虽然会弹出下载框,但是下载之后你发现打开时失败,文件路径错误;这是因为webview中下载文件出现套娃现象

uni.downloadFile({
                    url, //仅为示例,并非真实的资源
                    success: (res) => {
                        console.log('res', JSON.stringify(res));
                        if (res.statusCode === 200) {
                            var filePath = res.tempFilePath;
                            //#ifndef H5
                            uni.openDocument({
                                filePath: filePath,
                                showMenu: true,
                                success: function(res) {
                                    console.log('打开文档成功');
                                }
                            });
                            //#endif
                            //#ifdef H5
                                    window.location.href=filePath 
                            //#endif
                        }
                    }
                });

以上方法就无法打开了

解决方法:可以使用5+app的plus.downloader.createDownload(url,{})进行文件下载打开

function plusDownloader(url,options={}){
    return new Promise((result,reject)=>{
        let dtask=plus.downloader.createDownload(url, options, function(d, status){
            if(status == 200){ 
                console.log("Download success: " + JSON.stringify(d));
                plus.runtime.openFile(d.filename,(err)=>{
                    console.log(err);
                });
            } else {
                console.log("Download failed: " + status); 
            }  
        });
        dtask.start(); 
    })
    
}

调用方法:

plusDownloader(url,{filename:'_downloads/'+filename})

 

posted @ 2022-04-27 20:56  奔跑吧前端(李钊)  阅读(6520)  评论(0编辑  收藏  举报