export const updateUserAvatarResponse = async data => {const resp = await fetch(config.domain + '/api/users/update/avatar/', { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'multipart/form-data', 'Authorization': 'Bearer ' + data.access, }, body: new FormData('avatar', { type: 'image/jpeg', uri: data.avatar.uri, name: 'filename.jpg', // data: data.avatar, }),});return await resp.json();};updateAva = async () => { const options = { title: '选择头像', // noData: false, storageOptions: { //skipBackup: true, path: 'images', }, // multiple: true, //包括Exif:正确, }; ImagePicker.showImagePicker(options, (response) => { if (response.uri) { this.setState({ imageData: response.uri, data: response }); this.uploadImage(); } });};uploadImage = async () => { const access = await getAccessToken(); const resp = await updateUserAvatarResponse({ access: access, avatar: this.state.data }); console.log(this.state.data); console.log(resp);};但出现此错误。
1 回答
holdtom
TA贡献1805条经验 获得超10个赞
实际问题在这里:
body: new FormData('avatar', {
type: 'image/jpeg',
uri: data.avatar.uri,
name: 'filename.jpg',
// data: data.avatar,
}),
FormData
您正在尝试通过传递无效参数来创建新实例。根据FormData 的文档,当您传递HtmlElement
可以从中获取的值document.getElementById
(例如)时,这种创建 FormData 的方法是有效的。
根据您的情况,正确的创建方法FormData是:
let data = new FormData();
data.append('avatar', {
type: 'image/jpeg',
uri: data.avatar.uri,
name: 'filename.jpg',
});
添加回答
举报
0/150
提交
取消