问题描述
我试图将图像发送到后端(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
})