对于您的用例,您可以构建formData,例如:
let data = new FormData();
data.append('avt', {
name: '<file_name>',
type: '<file_type>', // e.g. 'image/png'
uri: '<file_uri>',
});
data.append('email', 'test@email.com');
如果您使用fetch 发出 API 请求,那么您的请求可以是:
fetch('<api_url>', {
method: '<api request method>',
body: data,
headers: {
'Content-Type': 'multipart/form-data',
// ...other headers
}
})
.then((response) => response.json())
.then((response) => {
console.log('upload file response: ', response)
})
.catch(() => {
console.log('upload file error: ', error);
});
如果您使用axios 发出 API 请求,那么您的请求可以是:
axios({
url: '<api_url>',
method: '<api request method>',
data: data,
})
.then((response) => {
console.log('upload file response: ', response)
})
.catch(() => {
console.log('upload file error: ', error);
});