Axios has the problem of uploading pictures.

because the parameters passed by axios are in json format, and the parameters in formdata format are accepted in the background,
axios.defaults.headers.post ["Content-Type"] =" application/x-www-form-urlencoded;charset=UTF-8";
import qs from "qs";
axios.defaults.transformRequest = [function (data) {
return qs.stringify (data)
}] is globally configured;
converts json data into form-data format with qs.
but I am now in a vue group to upload pictures. The code is as follows:
onRead (file) {

    console.log(file);
    let param = new FormData();
    param.append("file", file.file, file.file.name);
    param.append("userId", this.userInfo.userId);
    param.append("token", this.userInfo.token);
    let config = {
      header: {"Content-Type": "multipart/form-data"}
    }
    let that = this;
    this.$http.post( that.baseUrl + "/user/headPicImg", param, config).then((res) => {
      console.log(res);
      that.imgSrc = "http://116.62.208.141:8181/" + res.data.headImgPath || "../assets/imgs/gift.png";
    })
  },
  form-dataqs
  if(axios.method  === "post" && axios.data.constructor !== FormData){

axios.defaults.transformRequest = [function (data) {

return qs.stringify(data)

}];
}
places restrictions on the conversion conditions, but in this way, some of the interfaces that are not in form-data parameter format are within this judgment condition but are not converted to form-data format by qs. I would like to ask what is wrong with my judgment conditions and how to modify them.

Mar.01,2021

const service = axios.create({
  baseURL: baseUrl,
  // headers: {
  //  'Content-Type': 'application/x-www-form-urlencoded'
  // },
  transformRequest: [function (data) {
    // data['src'] = 'dev'
    if (data instanceof FormData) {
      return data
    }
    data = qs.stringify(data)
    return data
  }]
})
Menu