收藏
回答

wx.getFileSystemManager().readFile在开发者工具有效 在微信无效?

if (this.data.imageUrls.length > 0) {
      this.data.imageUrls.forEach((element) => {
        console.log(element.url, '单图片')
        wx.getFileSystemManager().readFile({
          filePath: element.url,
          success(res) {
            let boundary = "----abcdefg";
            let boundarystart = that.stringToBuffer(`--${boundary}\r\n`);
            let boundaryend = that.stringToBuffer(`\r\n--${boundary}--\r\n`);
            let fileheader = that.stringToBuffer(`Content-Disposition: form-data; name="files";filename="123.jpg";\r\nContent-Type:image/jpeg;\r\n\r\n`);
            let buffers = that.concatenate(boundarystart, fileheader, res.data, boundaryend);
            wx.request({
              data: buffers,
              url: 'https://www.baolegebei.com/api/lygim/upload_images',
              header: {
                'Authorization': wx.getStorageSync(cache.token),
                'content-type'`multipart/form-data; boundary=${boundary}`
              },
              method: 'post',
              success(res) {
                if (res.data.success) {
                  // console.log('上传成功', res.data.data[0])
                  let imgs = that.data.imgs;
                  imgs.push(res.data.data[0])
                  that.setData({
                    imgs
                  }, () => {
                    console.log(that.data.imgs)
                  })
                } else {
                  wx.showToast({
                    title: '有非法图片,禁止上传',
                    icon: 'error',
                    duration: 2000
                  })
                }
              },
              fail(error) {
                console.log(error);
                wx.showToast({
                  title: '上传失败',
                  icon: 'error',
                  duration: 2000
                })
              }
            })
          },
          fail(error) {}
        });
      })
    }
b背景: 小程序图片需要校验才能上线,wx.getFileSystemManager().readFile读取图片信息,后台校验。
w为什么读取在开发者工具 跟 微信小程序环境不一致呢,
r如下截图


两种环境下, 图片读取的url不太一样,   wx.getFileSystemManager().readFile貌似好像只能读取http://tmp/yGi0tYkuQcq8400b11db0f30e52a74b8da68f34ad42c.jpg,
不能读取 wxfile://tpm_2e23rewfwefdfvdf  这种类型图片地址
望大佬指点,谢谢
回答关注问题邀请回答
收藏

2 个回答

  • 青团社
    青团社
    2021-05-28

    这个东西怎么说呢。工具毕竟不是真机,一切以真机为准,你这个问题使用工具的真机调试看下

    2021-05-28
    有用
    回复
  • 張樹傑
    張樹傑
    2021-05-20
    stringToBuffer(text) {
        let enc = new TextEncoder();
        console.log("enc", enc.encode(text));
        return enc.encode(text);
      },
      concatenate(...arrays) {
        let length = 0;
        for (let i = 0; i < arrays.length; i++) {
          arrays[i] = new Uint8Array(arrays[i]) //全部转成Uint8Array
          length += arrays[i].length;
        }
        let result = new Uint8Array(length);
        let offset = 0;
        for (let array of arrays) {
          result.set(array, offset);
          offset += array.length;
        }
        return result.buffer;
      },
    
    y应该是stringToBuffer
    
    2021-05-20
    有用
    回复 1
    • GYF
      GYF
      2021-11-02
      楼主,这段代码是解决上面那个问题的?
      2021-11-02
      回复
登录 后发表内容