1.定义下载点击事件

<view class="button">
   <view class="submit" @click="download(lawInfo.Enclosure)"><text>下 载</text></view>
</view>
//下载文件
      download(e) {
        let that = this
        uni.downloadFile({
          url: imghost+'/StaticFiles'+ e , //下载地址,后端接口获取的链接
          success: (data) => {
            console.log(data.tempFilePath)
            console.log(JSON.stringify(data))
            if (data.statusCode === 200) {
              uni.saveFile({ //文件保存到本地
                tempFilePath: data.tempFilePath, //临时路径
                success: function(res) {
                  console.log("下载成功"+res.savedFilePath)
                  console.log(JSON.stringify(res))
                  uni.showToast({
                    icon: 'none',
                    mask: true,
                    title: '文件已保存!', 
                    duration: 3000,
                  });
                  uni.openDocument({
                    fileType: 'docx',
                    filePath: res.savedFilePath,
                    success: function(res) {
                      console.log('打开文档成功');
                    }
                  });
                }
              });
            }
          },
          fail: (err) => {
            console.log(err);
            uni.showToast({
              icon: 'none',
              mask: true,
              title: '失败请重新下载',
            });
          },
        });
      },

2.注意ios下载不能为空文件

3.ios小程序只能打开docx,xlsx,pptx等类型的文件

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐