uni-app的文件下载

1,332 阅读1分钟

uni-app的文件下载

uni.downloadFile({
    url:'', // 请求地址
    responseType:'arraybuffer',
    header:{  //请求头的配置
      
    },
    success:(data)=>{// 请求成功的回调
      //下载成功后我们需要将文件保存到本地
      wx.getFileSystemManager().saveFile({
         tempFilePath:data.tempFilePath,  //下载成功返回来的文件路径
         success:(res)=>{
             uni.showToast({title:'文件已保存:' + res.savedFilePath}) //保存成功的提示
             setTimeout(()=>{
                uni.openDocument({  // 打开文档的API
                   filePath:res.savedFilePath,  // 文件地址
                   fileType:'pdf' //打开的文件类型
                })
             },3000)
         }
      })
    },
    fail:(err)=>{  //下载失败的回调
      
    }
})