axios 上传图片, 图片信息已获取到,但是一上传就为空

Ying
  • 73

这是传入之前的打印

clipboard.png

departmentLogo是有值的..但是
传入接口后,参数变为空

clipboard.png

const waitTime=(url)=>{
    return new Promise((resolve, reject)=>{
        setTimeout(()=>{
            reject({msg:'数据响应超时',waitTime:url})
        },70000)
    })  
}

POST2: (url, params={}) => {
    return new Promise((resolve, reject) => {
        console.log(params)
        axios.post(url, params, {
            headers: {
                'Content-Type': 'multipart/form-data',
            }
        })
        .then(response => {
            const res = response.data;
            resolve(res)
        })
        .catch((error) => {
            Err(error);
            reject(error)
        })
    }) 
}


post2: (url, params) => {
    return new Promise((resolve, reject) => {
        Promise.race([XHR.POST2(url, params), waitTime(url)]).then(res => {
            resolve(res)
        }).catch(res => {
            notification.error({
                message: res.message
            })
            reject(res)
        })
    })
}

请问代码有哪里需要更改的,可以让传入后的值不为空吗

回复
阅读 1.9k
2 个回答

file格式 请用formData格式上传

上传文件的话 data需要是: FormData, File, Blob 三种之一
https://github.com/axios/axio...

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer
  data: {
    firstName: 'Fred'
  },

官方上传文件示例
https://github.com/axios/axio...

你知道吗?

宣传栏