微信小程序weui uploader的使用

js

uplaodFile(files) {
    // 文件上传的函数,返回一个promise
    return new Promise((resolve, reject) => {
      let that = this
      var urls = []
      console.log('upload files', files)
      wx.uploadFile({
        filePath: files.tempFilePaths[0],
        name: 'hwpic',		//key-value的key的名字
        url: 'http://localhost:8080/index/photo', //后端的接口
        success(res) {
          console.log(res)
          var data = JSON.parse(res.data)
          if (data.status == 1) {
            urls.push(data.url)	//必须返回一个url
            that.setData({
              imgUrl: urls,
            })
            resolve({
              urls:urls
            });
          }
        }
      })
      setTimeout(() => {
        reject('some error')
      }, 1000)
    })
  },

html

 <!-- 图片上传内容 -->
    <view class="page__bd">
        <mp-cells>
            <mp-cell>
                <mp-uploader bindfail="uploadError" bindsuccess="uploadSuccess" select="{{selectFile}}" upload="{{uplaodFile}}" files="{{files}}" max-count="9" title="图片上传" tips="图片上传提示"></mp-uploader>
            </mp-cell>
        </mp-cells>
    </view>
</view>

json

{
  "usingComponents": {
   "mp-uploader": "../../components/uploader/uploader"
  }
  }
上一篇:QA在网站建设中的作用


下一篇:华为AppTouch携手全球运营商,助力开发者出海