Dolen

导航

uni-app下载文件

下载方式一:

plus.runtime.openURL(fileUrl)

下载方式二:

        uni.downloadFile({
          url: fileUrl,
          success: (res) => {
            const filePath = res.tempFilePath;
            if (res.statusCode === 200) {
              this.isShowProgress = false;
              console.log('下载成功');
            }
            uni.saveFile({
              tempFilePath: filePath,
              success: (res1) => {
                this.isShowProgress = false;
                var saveFilePath = res1.savedFilePath;
                uni.showModal({
                  title: "提示",
                  content: "文件已保存:" + saveFilePath,
                  cancelText: "我知道了",
                  confirmText: "打开文件",
                  success: (res2) => {
                    if (res2.confirm) {
                      uni.openDocument({
                        filePath: saveFilePath,
                        success: (res) => {
                          console.log("打开文档成功", JSON.stringify(res))
                        }
                      })
                    }
                  }
                })
              }
            })
          }
        })

完整代码:

previewFile(item) {
        let fileUrl = item.fileUrl
        // 下载方式一:
// plus.runtime.openURL(fileUrl) this.isShowProgress = true; // 下载方式二: uni.downloadFile({ url: fileUrl, success: (res) => { const filePath = res.tempFilePath; if (res.statusCode === 200) { this.isShowProgress = false; console.log('下载成功'); } uni.saveFile({ tempFilePath: filePath, success: (res1) => { this.isShowProgress = false; var saveFilePath = res1.savedFilePath; uni.showModal({ title: "提示", content: "文件已保存:" + saveFilePath, cancelText: "我知道了", confirmText: "打开文件", success: (res2) => { if (res2.confirm) { uni.openDocument({ filePath: saveFilePath, success: (res) => { console.log("打开文档成功", JSON.stringify(res)) } }) } } }) } }) } }) }

 

posted on 2022-12-06 22:19  Dolen  阅读(1305)  评论(0编辑  收藏  举报