收藏
回答

请问小程序在windows的pc端没有权限读取本地文件要如何解决?

在windows的pc端直接调用wx.setStorageSync('userToken', token)就会报错。

同时,使用wx.chooseImage,然后再wx.uploadFile也同样报错,原因是chooseImage会写入wxfile临时目录,这同样无法写入。


    /**

     * 拍照/选图

     */

    onChooseImage() {

      const remaining = 5 - this.data.imageList.length;

      if (remaining <= 0) {

        wx.showToast({

          title: '已达到上传上限',

          icon: 'none'

        });

        return;

      }


      wx.chooseImage({

        count: remaining, // 根据已上传的数量限制剩余可上传数量

        sizeType: ['original', 'compressed'],

        sourceType: ['camera'], // 仅使用相机

        success: (res) => {

          const tempFilePaths = res.tempFilePaths;

      

          if (tempFilePaths.length > 0) {

            console.log('选择的图片路径:', tempFilePaths);

      

            let newImageList = this.data.imageList.concat(tempFilePaths);

            if (newImageList.length > 5) {

              newImageList = newImageList.slice(0, 5);

              wx.showToast({

                title: '最多可上传5张照片',

                icon: 'none',

              });

            }

      

            this.setData({

              imageList: newImageList,

              mainPreview: newImageList[newImageList.length - 1], // 默认将最后一张作为大图预览

            });

      

            // 遍历每张图片进行直接上传

            tempFilePaths.forEach((filePath, index) => {

              console.log(`开始上传第 ${index + 1} 张图片,路径: ${filePath}`);

      

              // 上传图片

              wx.uploadFile({

                url: `${backendApiUrl}/application/me/updateProof`, // 替换为实际的上传地址

                filePath,

                name: 'proofImages',

                header: {

                  Authorization: `Bearer ${getUserToken()}`, // 确保用户登录并获取 Token

                },

                formData: {

                  id: '123', // 替换为实际的申请 ID

                },

                success: (uploadRes) => {

                  console.log(`第 ${index + 1} 张图片上传成功,响应:`, uploadRes);

      

                  wx.showToast({

                    title: `第 ${index + 1} 张图片上传成功`,

                    icon: 'success',

                  });

                },

                fail: (uploadErr) => {

                  console.error(`第 ${index + 1} 张图片上传失败:`, uploadErr);

                  wx.showToast({

                    title: `第 ${index + 1} 张图片上传失败`,

                    icon: 'none',

                  });

                },

              });

            });

          }

        },

        fail(err) {

          console.error('chooseImage fail:', err);

          wx.showToast({

            title: '拍照失败,请重试',

            icon: 'none',

          });

        },

      });

    },

这些问题在ios、安卓以及MAC端都没有问题,windows端会有问题。

回答关注问题邀请回答
收藏
登录 后发表内容