原生Input上传文件到七牛

				<input
                    type="file"
                    class="upload_input"
                    ref="upFile"
                    @change="fileChange(e, scope.row)"
                    accept="image/png, image/jpeg, image/gif, image/jpg"
                  />


.......................................
 fileChange (e, val) { // 上传
 	//获取文件
      let evt = e || event
      let that = this
      let file = evt.target.files[0]
      console.log(evt, val)
      let strList = file.name.split('.')
      let fileType = strList[strList.length - 1]
      //获取七牛的token和key
      this.$http.cloudGetToken(51, fileType).then(res => {
        if (res.status == 1) {
          this.uploadFile(file, res.result, val)
        }
      })
    },
    uploadFile (file, { uptoken, fileKey }, val) {
      const fd = new FormData()
      fd.append('token', uptoken)
      fd.append('key', fileKey)
      fd.append('file', file)
      var xhr = new XMLHttpRequest()
      var that = this
      //向七牛发送请求
      xhr.open('POST', '七牛的地址', true)
      return new Promise(resolve => {
        xhr.onload = function () {
          if (xhr.readyState === 4) {
            if (xhr.status === 200) {
            //获取七牛返回的文件信息
              var data = JSON.parse(xhr.responseText)
              console.log(data, 'data')
              if (data.result.w > 128 || data.result.height > 128) {
                that.$Message.error('图片大小超过128*128!')
                return
              }
              //向后台发送请求
              let params = {
                normId: val.id, fType: that.typeList[that.typeIndex].type, schoolCode: that.user.schoolCode, imgUrl: data.result.url
              }
              console.log(params)
              that.$cloudRequest('/sxxxxx/xxxxxxx/xxxxxx', params, 'get').then(res => {
                if (res.status == 1) {
                  console.log(data)
                  val.icon = data.result.url
                  console.log(val, 'val')
                }
              })
            } else {
              console.log(xhr.statusText)
            }
          }
        }
        xhr.onerror = function (e) {
          console.log(xhr.statusText)
        }
        xhr.send(fd)
      })
    }

                  ```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值