Javascript 将用户给定的图像发送到React中的Api
我尝试将图像作为输入,它将作为formData发送到api 我的代码-> 对于用户输入->Javascript 将用户给定的图像发送到React中的Api,javascript,reactjs,file-upload,multipartform-data,Javascript,Reactjs,File Upload,Multipartform Data,我尝试将图像作为输入,它将作为formData发送到api 我的代码-> 对于用户输入-> <input type="file" accept="image/*" name="image-upload" id="SelectFile" onChang
<input
type="file"
accept="image/*"
name="image-upload"
id="SelectFile"
onChange={this.imageHandler}
style={{ display: 'none' }}
/>
saveImage函数调用api->
export const saveImage = async (data,email) => {
try {
const response = await axios.post(`${commonApi.api}/user-image`, data, {
headers: {
'content-type': 'multipart/form-data',
'Authorization': localStorage.getItem('token')
},
params:{
userEmail: email
}
});
if (response.data.success) {
return { success: true, message: '', data: response.data.payload };
} else {
return { success: false, message: response.data.message };
}
} catch (error) {
console.log('response',error.response.data)
return { success: false, message: 'Failed' };
}
};
这是一个403错误。
但当我与邮递员核对时,它工作正常
邮递员请求图像->
所以我认为我处理图像的方式不对。
我在这里犯了什么错误?在浪费了2个多小时后发现了我的问题<标题中的代码>授权似乎区分大小写。 我写了->
headers: {
'content-type': 'multipart/form-data',
'Authorization': localStorage.getItem('token')
},
哪个应该是->
headers: {
'content-type': 'multipart/form-data',
'authorization': localStorage.getItem('token')
},
对于A
xD,2小时
headers: {
'content-type': 'multipart/form-data',
'authorization': localStorage.getItem('token')
},