Riunshow / NoteBook

人生不如意事十之九八,可与人言者并无二三
3 stars 0 forks source link

上传图片, 将file转为buffer再转为blob #12

Open Riunshow opened 5 years ago

Riunshow commented 5 years ago
<input ref="inputFile" type="file" @change="onUploadImg" v-if="!disabled" :accept="accept" />
    // 上传图片
    onUploadImg (event) {
      let imgBlob = null
      const img = event.target.files[0]
      const reader = new FileReader()
      reader.readAsArrayBuffer(img)
      reader.onload = function () {
        imgBlob = new Blob([this.result])
        this.uploadMethod(imgBlob)
      }

      // this.cropperImg = URL.createObjectURL(img)
    },

request

    let data = {
        content: blob,
        tenant: '',
        domain: this.domain
      }
  uploadPic (url, data, header) {
    let formData = new FormData()
    let keys = Object.keys(data)
    keys.forEach(key => {
      formData.append(key, data[key])
    })
    return axios.post(url, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
        ...header
      }
    })
  }