轴。如何使用axios上传图片到Node.js
我之前使用 ApiSauce
通过 Multer
将清单从 React Native
应用程序发布到 Node.js
,现在我已迁移到 Axios,除了上传图像部分之外,一切都很顺利。
export const add = (listing, onUploadProgress) => {
const data = new FormData();
data.append('title', listing.title);
data.append('price', listing.price);
data.append('categoryId', listing.category.value);
data.append('description', listing.description);
data.append('userId', listing.userId);
listing.images.forEach((image, index) =>
data.append('images', {
name: 'image' + index,
type: 'image/jpeg',
uri: image,
}),
);
if (listing.location)
data.append('location', JSON.stringify(listing.location));
return client.post(endpoint, data, {
onUploadProgress: (progress) =>
onUploadProgress(progress.loaded / progress.total),
});
};
I was previously using ApiSauce
to post listings from React Native
App to Node.js
with Multer
, and now I have migrated to Axios
everything went fine except for the uploading images part.
export const add = (listing, onUploadProgress) => {
const data = new FormData();
data.append('title', listing.title);
data.append('price', listing.price);
data.append('categoryId', listing.category.value);
data.append('description', listing.description);
data.append('userId', listing.userId);
listing.images.forEach((image, index) =>
data.append('images', {
name: 'image' + index,
type: 'image/jpeg',
uri: image,
}),
);
if (listing.location)
data.append('location', JSON.stringify(listing.location));
return client.post(endpoint, data, {
onUploadProgress: (progress) =>
onUploadProgress(progress.loaded / progress.total),
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试删除所有内容类型。对于多部分边界错误,
请尝试像此其余部分由 multer 处理
you can try to remove all content type. for multipart boundary error
try like this rest is handled by multer