axios 进行文件上传

async uploadFile () {
      var config = {
        onUploadProgress: (progressEvent) => {
          let percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
          // this.uploadPercentage = percentCompleted + '%'
          console.log(percentCompleted)
        }
      }
      let formData = new FormData()
      let form = document.getElementById('headImg')
      let file = form.files[0]
      formData.append('file', file)
      let Url = window.URL || window.webkitURL
      var imgURL = Url.createObjectURL(file)
      this.setState({
        headHref: imgURL
      })
      // this.uploadHeader(formData);
      let res = await axios.post('/user/head/upload', formData, config).then(res => {
        this.ticket = res.data.image
        // this.state.upData[tag] = logoTicket
        // this.checkIfCanCommit()
      }).catch(err => { console.error(err) })
      axios.post('user/head/update', {
        head_file: res.data.data.head_file
      })
    }

你可能感兴趣的:(Ajax,JavaScript)