轴。如何使用axios上传图片到Node.js

发布于 2025-01-11 10:00:55 字数 875 浏览 2 评论 0原文

我之前使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2025-01-18 10:00:55

您可以尝试删除所有内容类型。对于多部分边界错误,

请尝试像此其余部分由 multer 处理

axios.post(`${subURL}/upload-avatar`, formData, { headers: { "Authorization": jwt } })
        .then(response => {
            return response.data
        })
        .catch(err => {
            throw err
        })

you can try to remove all content type. for multipart boundary error

try like this rest is handled by multer

axios.post(`${subURL}/upload-avatar`, formData, { headers: { "Authorization": jwt } })
        .then(response => {
            return response.data
        })
        .catch(err => {
            throw err
        })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文