uniapp 文件下载保存本地在文件管理中查看

2022-07-01  本文已影响0人  流浪的風
// file:{filename:文件名称,fileulr:文件地址}
 export default function dowloadFile(file){
     let type = file.fileurl.indexOf('.docx')>0?'.docx':'.doc'
        //file://storage/emulated/0  固定写法,
        //Download:文件保存的文件夹名称,试了其他名称但是在手机文件管理中没能找到文件,使用download后可以
        //type:文件后缀
     let task = plus.downloader.createDownload(file.fileurl,{
        filename:'file://storage/emulated/0/Download/'+file.filename+type
     },function(d,status){
        uni.hideLoading()
        if(status === 200){
            uni.showToast({
                icon:'none',
                title:'下载成功',
            })
            //d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
            let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename)
            plus.runtime.openFile(d.filename)//选择软件打开文件
        }else{
            uni.showToast({
                icon:'none',
                title:'下载失败成功',
            })
            plus.downloader.clear()
        }
     })
     uni.showLoading({
        title:'文件下载中...'
     })
     task.start();
 }
上一篇 下一篇

猜你喜欢

热点阅读