uniapp-文件下载

需求:

实现下载文件

 uni.downloadFile({
        // 注意:url是完整的下载地址,并不仅仅是api
        // 例如:http://192.168.xx.xx:8080/manange/xx/xx/?file=xxxxxxxx
        url:
          getBaseUrl("") +
          "/manage/manarchive/fileDownload" +
          // 参数
          turnObjToUrl({
            technical: data.path,
          }), 
        header: {
          "Content-Type": "application/json",
          Authorization:
            uni.getStorageSync("token_type") +
            " " +
            uni.getStorageSync("access_token"),
        },
        success: (data) => {
        // 返回结果示例
        // {
	    //    errMsg: "downloadFile:ok",
	    //    statusCode: 200,
	    //    tempFilePath: "blob:http://localhost:8080/80fe5aaf-a90f-4a77-bb37-acb635665444"
        // }
          if (data.statusCode === 200) {
            uni.showToast({
              icon: "none",
              mask: true,
              title: "下载成功!",
              duration: 2000,
            });
            setTimeout(() => {
              // 新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
              uni.openDocument({
                // tempFilePath接口返回的临时下载路径
                filePath: this.fileNameEscape(data.tempFilePath),
                success: function (res) {
                  console.log('打开文档成功');
                },
                fail() {
                  uni.showToast({
                    icon: "none",
                    mask: true,
                    title: "暂不支持此类型",
                    duration: 2000,
                  });
                },
              });
            }, 2 * 1000);
          } else {
            console.log("data.statusCode", data.statusCode);
          }
        },
        fail: (err) => {
          uni.hideLoading();
          console.log(err);
          uni.showToast({
            icon: "none",
            mask: true,
            title: "下载失败,请重试!",
          });
        },
      });
/**
 * ios可能显示不出来,可以在IOS端下进行单独处理,使用escape
 * ios下文件名中文处理
 * @param {String} filename
 */
fileNameEscape(filename) {
    if (uni.getSystemInfoSync().platform == "ios") {
        return escape(filename);
    }
    return filename;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值