axios传参?

axios.post('http://localhost:8080/pic/upload', 
param,{'Content-type' : 'multipart/form-data'}, 
_this.user.userId,
)

这样子传参哪里不对?

阅读 2.1k
1 个回答

对于文件上传,你需要使用 FormData 对象,把文件对象 append 进去。然后交给 axios.post 的第二个参数就可以了,如果你还需要传递其他额外的参数,也加在 FormData 里面。


// 这里的 fileInput 是你的输入框对象。

const file = fileInput.files[0]

const form = new FormData()
form.append('file', file)
form.append('user_id', _this.user.userId)

axios.post('http://localhost:8080/pic/upload', form).then(result => {
    console.log(result)
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进