收藏
回答

为什么我用wx.chooseMessageFile上传的文件只能上传一个?请大神指导

我在网上看到别人的代码,但是只能上传一张,搞来搞去不明白,请大神指导。代码如下:

<button type="primary" style="margin-top: 105rpx;" bindtap="uploadFileTap" data-type="file">上传文件</button>

<button type="primary" style="margin-top: 45rpx;" bindtap="uploadFileTap" data-type="img">上传图片</button>

// pages/uploadFile/uploadFile.js

Page({


  /**

   * 页面的初始数据

   */

  data: {},


  /** 上传按钮点击监听 */

  async uploadFileTap(res) {

    // 上传类型

    const type = res.currentTarget.dataset.type

    let filePathObj = null

    let filePathList = []


    if (type == 'file') {

      filePathObj = await this.chooseMessageFile(1, 'file')

      if (!filePathObj) return

      filePathList.push(filePathObj.tempFiles[0].path)

    } else if (type == 'img') {

      filePathObj = await this.chooseImg(2)

      if (!filePathObj) return

      filePathList = filePathObj.tempFilePaths

    } else {

      return

    }


    console.log("选择文件信息 ====>", filePathObj)


    let cloudPathList = []


    for (let i = 0; i < filePathList.length; i++) {

      const cloudPathObj = await this.upLoadFile(filePathList[i], 'file')

      if (!cloudPathObj) {

        continue

      }

      console.log(filePathList[i], "文件上传成功=====>", cloudPathObj)

      cloudPathList.push(cloudPathObj.fileID)

    }


    console.log("最终返回云文件ID列表 =====>", cloudPathList)


  },


  /**

   * 从聊天记录选择文件

   * @param {number} count 可选择数量(1-100)

   * @param {string} type 可选择文件类型 all:全部类型 video: 仅视频 image: 仅图片 file: 除了视频、图片外的文件类型

   */

  chooseMessageFile(count, type) {

    return new Promise((resolve, reject) => {

      wx.chooseMessageFile({

        count: count,

        type: type,

        success(res) {

          resolve(res)

        },

        fail(err) {

          console.log("选择文件错误 =====>", err)

          resolve(false)

        }

      })

    })

  },


  /** 选择图片封装函数

   * @param count 照片数量

   * @param sizeType 照片的质量, 默认 ['original', 'compressed']

   * @param sourceType 照片来源, 默认 ['album', 'camera']

   */

  chooseImg(count, sizeType, sourceType) {

    if (!count) count = 1

    if (!sizeType) sizeType = ['original', 'compressed']

    if (!sourceType) sourceType = ['album', 'camera']

    return new Promise((resolve, reject) => {

      wx.chooseImage({

        count: count,

        sizeType: sizeType,

        sourceType: sourceType,

        success(res) {

          resolve(res)

        },

        fail(err) {

          resolve(false)

          console.error("===== 选取照片失败 =====", err)

        }

      })

    })

  },


  /** 

   * 上传文件封装函数, 文件名随机性处理,由17位随机字符+13位时间戳组成

   * @param {string} filePath 要上传图片的临时路径

   * @param {string} cloudPathPrefix 云数据库存储文件路径前缀

   */

  upLoadFile(filePath, cloudPathPrefix) {

    // 取随机名

    let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    let randomStr = '';

    for (let i = 17; i > 0; --i) {

      randomStr += str[Math.floor(Math.random() * str.length)];

    }

    randomStr += new Date().getTime()


    return new Promise((resolve, reject) => {

      let suffix = /\.\w+$/.exec(filePath)[0] //正则表达式返回文件的扩展名

      let cloudPath = cloudPathPrefix + '/' + randomStr + suffix

      wx.cloud.uploadFile({

        cloudPath: cloudPath,

        filePath: filePath,

        success(res) {

          resolve(res)

        },

        fail(err) {

          resolve(false)

          console.error("===== 上传文件失败 =====", err)

        },

      })

    })

  },

})


回答关注问题邀请回答
收藏

6 个回答

  • 🇪 🇱 🇸 🇪
    🇪 🇱 🇸 🇪
    2023-02-01

    改一下选择数量

    2023-02-01
    有用 1
    回复
  • Jianbo
    Jianbo
    2023-01-31

    方法调用的时候就只传递了1,当然只能上传一个文件

    2023-01-31
    有用 1
    回复
  • 那一抹笑😃 穿透阳光
    那一抹笑😃 穿透阳光
    2023-02-01

    选择的时候,能选择多个,还是一个

    2023-02-01
    有用
    回复 9
    • 刘金华
      刘金华
      发表于移动端
      2023-02-01
      选择了多个文件
      2023-02-01
      回复
    • 那一抹笑😃 穿透阳光
      那一抹笑😃 穿透阳光
      2023-02-01回复刘金华
      这个地方一直取得是数组中的第一个,应该循环
      2023-02-01
      回复
    • 刘金华
      刘金华
      发表于移动端
      2023-02-01
      好的,谢谢您,我再试试
      2023-02-01
      回复
    • 刘金华
      刘金华
      2023-02-01
      怎么循环,麻烦您指导指导
      2023-02-01
      回复
    • 那一抹笑😃 穿透阳光
      那一抹笑😃 穿透阳光
      2023-02-02回复刘金华
      你不是技术吗
      2023-02-02
      回复
    查看更多(4)
  • 刘金华
    刘金华
    2023-02-01

    就是因为我已经把 filePathObj = await this.chooseMessageFile(1, 'file')里的1改成了10,结果还是上传一个文件呀,所以百思不得其解,恳请大神指导

    2023-02-01
    有用
    回复
  • 一笑皆春
    一笑皆春
    2023-02-01

    你调用的时候写了1,所有只能1个啊,都不检查代码就发问吗?

    2023-02-01
    有用
    回复
  • showms
    showms
    2023-02-01

    额...

    2023-02-01
    有用
    回复
登录 后发表内容