微信小程序下载文件名问题

wx.saveFile和wx.getFileSystemManager().saveFile的区别

wx.saveFile之后调用wx.openDocument得到的是临时文件地址和临时文件名,原文件名就没了,所以先保存原文件名。再用wx.getFileSystemManager().saveFile将文件名替换回来

downLoad1: function(e) {
    let dataset = e.currentTarget.dataset
    let idx = dataset.idx;
    var that = this;
    let uri = that.data.beforeList[idx].fileUri;
    let fileName = that.data.beforeList[idx].fileName;
    wx.downloadFile({
      url: uri,
      success: function(res) {
        console.log(res);
        const manage = wx.getFileSystemManager();
        if (res.statusCode === 200) {
          manage.saveFile({
            tempFilePath: res.tempFilePath,
            filePath: wx.env.USER_DATA_PATH + "/" + fileName,
            success: function(res) {
            }
          });
          // 打开文档
          wx.openDocument({
            filePath: wx.env.USER_DATA_PATH + "/" + fileName,
            success: function(res) {
              console.log('打开文档成功')
            },
            fail: function() {
              console.log('打开失败');
            }
          })
        }
        // // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
        // if (res.statusCode === 200) {
        //   // 将临时地址转存到本地缓存中
        //   wx.saveFile({
        //     tempFilePath: res.tempFilePath,
        //     success: function(res) {
        //       console.log(res);
        //       var savedFilePath = res.savedFilePath;
        //       console.log('文件已下载到' + savedFilePath);
        //       // 查看下载的文件列表
        //       wx.getSavedFileList({
        //         success: function(res) {
        //           console.log(res);
        //         }
        //       })
        //       // 打开文档
        //       wx.openDocument({
        //         filePath: savedFilePath,
        //         success: function(res) {
        //           console.log('打开文档成功')
        //         },
        //         fail: function() {
        //           console.log('打开失败');
        //         }
        //       })
        //     }
        //   })
        // }
      }
    })
  },

注释部分的问题就是下载文件打开后文件名有问题,且文件类型也不对

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值