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

// 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();
 }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容