简体   繁体   English

如何保存从 NextJS 客户端发送的图像到我的 NodeJS 服务器?

[英]How can I save an image sent from my NextJS client to my NodeJS server?

I was trying a few weeks ago to upload an image to my NodeJS server and, from there, save it to an S3 bucket, but I'm stuck on the first part and my server doesn't receive the image.几周前,我尝试将图像上传到我的 NodeJS 服务器,然后从那里将其保存到 S3 存储桶,但我被困在第一部分,我的服务器没有收到图像。 I'm using multer for saving it in the public/ folder.我正在使用multer将其保存在public/文件夹中。

This is what I have in my client:这是我在我的客户中拥有的:

const onSubmit = async (data: any) => {
        const image = data.file[0];
        const params: RequestInit = {
            method: 'POST',
            headers: {
                'Content-Type': `multipart/form-data`,
                'Accept': 'image/*'
            },
            body: JSON.stringify(data)
        };
        await fetch('/api/post/create-post', params);
        // Upload image to S3 Bucket
        // Save post to db
    }

I was setting a boundary in the Content-Type with data=${JSON.stringify(data)} , and when I log the headers of my request before trying to save the image, there is no "boundary" and instead I receive this error Error: Multipart: Boundary not found (This error is shown in my server console)我用data=${JSON.stringify(data)}Content-Type中设置了一个边界,当我在尝试保存图像之前记录我的请求的标头时,没有“边界”,而是我收到了这个错误Error: Multipart: Boundary not found (此错误显示在我的服务器控制台中)

And this is what I have in my server:这就是我在我的服务器中拥有的:

const upload = multer({
    dest: 'public/'
})

const router = express.Router();

router.post('/', (req, res, next) => {
    console.log(req.headers);
    next();
}, upload.single('photo'), (req, res) => {
    console.log(req.file);

    res.json({ success: true });
})

module.exports = router;

You can remove the content type from the Axios headers and let Axios set it by itself.您可以从 Axios 标头中删除内容类型,并让 Axios 自行设置。

const onSubmit = async (data: any) => {
        const image = data.file[0];
        const params: RequestInit = {
            method: 'POST',
            body: JSON.stringify(data)
        };
        await fetch('/api/post/create-post', params);
        // Upload image to S3 Bucket
        // Save post to db
    }

You can check this link for the boundaries error.您可以检查链接以了解边界错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的 nextjs typescript(接口/类型)与 nodejs 服务器 typescript 模型(接口/类型)一起保存? - How I can keep my nextjs typescript (interfaces / types) with nodejs server typescript models (interfaces / types)? 如何从我的 nodejs 服务器中删除文件 - How can i delete a file from my nodejs server 我怎样才能在我从客户那里收到的 express 服务器中保存一个 img? - How could I save an img in my express server that I receive from my client? 我可以将请求者设置为不是我自己的服务器而是 nodeJS 上的客户端吗? - Can I set requestor as not my own server but Client on nodeJS? 无法从Heroku上的NodeJS应用程序将日志保存到服务器 - Can't save logs to server from my nodejs app on heroku 如何使用服务器域名从本地计算机访问我的nodejs Web服务器? - How can I access my nodejs web server from my local computer using the server domain name? 如何在服务器的控制台中访问我的nodeJS应用程序? - How can I access to my nodeJS app in console on my server? 如何通过Web服务调用将文件保存到我的Node.js服务器 - How do I save a file to my nodejs server from web service call 如何在服务器上的 express 和 node 中创建文件,然后将其下载到我的客户端。 我正在为我的前端和后端使用 NextJS - How do I create a file in express and node on my server and then download it to my client. I am using NextJS for my frontend and backend 如何使用socketio将数据从客户端传递到我的nodejs服务器? - How do I pass data from the client-side to my nodejs server using socketio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM