前端使用axios上传文件


upload:  (event) => {
  var file = event.target.files[0]
  /* eslint-disable no-undef */
  var param = new FormData() // 创建form对象
  param.append('head_img', file, file.name) // 通过append向form对象添加数据
  var headers = {
    'Content-Type':'multipart/form-data',
    'Authorization': 'bearer '+ window.localStorage.getItem('token'),
  };
  axios({
    method: 'post',
    url: this.$store.state.config.host + 'user/uploadimg',
    data: param,
    headers: headers,
  }).then( (value) => {
    response = response.data;
    this.$store.commit('setState', {
      noticeMsg: response.error_msg
    })
    // todo something...
  }).catch( (error) => {
    this.$store.commit('setState', {
      noticeMsg: '上传成功'
    })
    // todo something...
  });
},