微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用于将数据发送到multer的反应代码中的问题

如何解决用于将数据发送到multer的反应代码中的问题

我试图将图像发送到后端(multer) 我检查了多次后端代码似乎正确,我的前端代码有问题吗? POST 代码片段


const response = await fetch('http://localhost:5000/api/users/signup',{
                method:'POST',headers:{
                    'Content-Type': 'application/json'
                },body:JSON.stringify({
                    name: data.get('name'),email: data.get('email'),password: data.get('password'),image:data.get('image')
                  })
            });

完整的 auth.js 代码 https://pastebin.com/MHdDRtAX

解决方法

我认为您想使用 #include<iostream> #include<fstream> using namespace std; int main() { ifstream myFile("myfile.txt"); string x; while(myFile >> x) { // Do something with x // No checks needed! } } 对象。

FormData
,

您的内容类型不匹配。使用multer时,需要使用'Content-Type': 'multipart/form-data'。 基本思想是使用 FormData 对象。

下面的例子

const data = new FormData(event.target) 
const response = await fetch('http://localhost:5000/api/users/signup',{
                    method:'POST',headers:{
                        'Content-Type': 'multipart/form-data'
                    },body: data
                });

在网络选项卡中,您将找到带有键值的表单数据 图像应该是一个文件(二进制)

在multer中,示例代码如下

var express = require('express')
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })
 
var app = express()    
app.post('api/users/signup',upload.single('image'),function (req,res,next) {
    // req.file is the `image` file
    // req.body will hold the text fields,if there were any
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。