收藏
回答

为什么我做的提交图片一直是提交中?

//uploadPilot.js 自营点上传图书
//获取应用实例
var app = getApp()
const db = wx.cloud.database()
Page({
    data: {
        bookInfo:null,
        hidden:1,
        tempImg: [],
        fileIDs: [],
    },
    submitfunction () {
        wx.showLoading({
          title'提交中',
        })
        const promiseArr = []
        //只能一张张上传 遍历临时的图片数组
        for (let i = 0; i < this.data.tempImg.length;i++) {
          let filePath = this.data.tempImg[i]
          let suffix = /\.[^\.]+$/.exec(filePath)[0]; // 正则表达式,获取文件扩展名
          //在每次上传的时候,就往promiseArr里存一个promise,只有当所有的都返回结果时,才可以继续往下执行
          promiseArr.push(new Promise((reslove,reject)=>{
            wx.cloud.uploadFile({
              cloudPathnew Date().getTime() + suffix,
              filePath: filePath, // 文件路径
            }).then(res => {
              // get resource ID
              console.log(res.fileID)
              this.setData({
                fileIDsthis.data.fileIDs.concat(res.fileID)
              })
              reslove()
            }).catch(error => {
              console.log(error)
            })
          }))
        }
        Promise.all(promiseArr).then(res=>{
          db.collection('comments').add({
            data: {
              fileIDsthis.data.fileIDs //只有当所有的图片都上传完毕后,这个值才能被设置,但是上传文件是一个异步的操作,你不知道他们什么时候把fileid返回,所以就得用promise.all
            }
          })
            .then(res => {
              console.log(res)
              wx.hideLoading()
              wx.showToast({
                title'提交成功',
              })
            })
            .catch(error => {
              console.log(error)
            })
        })
      },
   
    uploadImgHandlefunction () {
        wx.chooseImage({
          count9,
          sizeType: ['original''compressed'],
          sourceType: ['album''camera'],
          success:res=> {
            // tempFilePath可以作为img标签的src属性显示图片
            const tempFilePaths = res.tempFilePaths
            this.setData({
              tempImg: tempFilePaths
            })
          }
        })
      },


    //事件处理函数
    onLoadfunction (options{


    },
    onReadyfunction () {


    },



    




})


<button type="warning" bindtap='uploadImgHandle'>上传图片</button>
<view>
  <block wx:for="{{tempImg}}" wx:key="{{index}}tmpimg">
    <image src='{{item}}' mode='aspectFill'></image>
  </block>
</view>
<button type="primary" bindtap='submit'>提交</button>


报错代码是  reslove()
            }).catch(error => {
              console.log(error)
回答关注问题邀请回答
收藏

9 个回答

  • 青梅
    青梅
    发表于移动端
    2020-11-06
    提交图片
    2020-11-06
    有用 1
    回复
  • lixueli
    lixueli
    发表于移动端
    2021-03-11
    提交一个小时怎么还 没有成功?
    2021-03-11
    有用
    回复
  • 云华
    云华
    发表于移动端
    2021-01-16
    为什么提交了一直在提交中呀?
    2021-01-16
    有用
    回复
  • 于丽荣
    于丽荣
    发表于移动端
    2021-01-10
    是的一直在提交中
    2021-01-10
    有用
    回复
  • 郭桂兰
    郭桂兰
    发表于移动端
    2021-01-08
    为什么我做的提交图片一直是提交中
    2021-01-08
    有用
    回复
  • 潘素云
    潘素云
    发表于移动端
    2021-01-08
    提交总是在转,提交不上,,,
    2021-01-08
    有用
    回复
  • 施正红
    施正红
    发表于移动端
    2021-01-08
    不知道
    2021-01-08
    有用
    回复
  • 王桂英
    王桂英
    发表于移动端
    2021-01-08
    填的不合格
    2021-01-08
    有用
    回复
  • 守章
    守章
    发表于移动端
    2020-11-10
    上有错字,重新再写。
    2020-11-10
    有用
    回复
登录 后发表内容
问题标签