想问问小程序下载文件机制,我为嘛感觉这么难下载呢 ?求个社区人员给回复下
发布于 6 年前 作者 fang54 7768 次浏览 来自 官方Issues

最近做小程序开发。需要下载文档到本地,但是格式不支持 导致都没法在手机预览和转发下载 请问还有办法吗,我们公司得这些文件格式特殊 没法使用官方得 wx.openDocument

去打开,再转发下载

1 回复
  randomString(len) {
    len = len || 32;
    var $chars =
      "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length;
    var pwd = "";
    for (let i = 0; i < len; i++) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
  },
  openDoc: function (e) {
    this.openDocTag = true;
    var index = e.currentTarget.dataset.index;
    var filePath = this.data.thread_data.doc_list[index].fileFullPath;
    var fileExtName = `.${this.data.thread_data.doc_list[index].fileType}`;
    let that = this;
    wx.showLoading({
      title: "正在下载..", //提示的内容,
      mask: true, //显示透明蒙层,防止触摸穿透,
      success: (res) => { },
    });
    const randfile = that.randomString(32) + new Date().getTime() + fileExtName;
    const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
    wx.downloadFile({
      url: filePath,
      filePath: newPath,
      success: (res) => {
        wx.hideLoading();
        wx.showLoading({
          title: "正在打开..", //提示的内容,
          mask: true, //显示透明蒙层,防止触摸穿透,
          success: (res) => { },
        });
        wx.openDocument({
          filePath: newPath,
          showMenu: true,
          success: (res) => {
            wx.hideLoading();
            console.log("open res", res);
          },
          fail: function (res) {
            console.log("open fail", res);
            wx.hideLoading();
          },
        });
      },
      fail: (res) => {
        console.log("res error", res);
        wx.hideLoading();
      },
      complete: (res) => {
        console.log("res complete", res);
      },
    });
  },
回到顶部