formdata只能正确发布文件,但会删除所有字符串。 (使用node,express和multer)

我尝试通过multer通过node.js上传formdata。

我测试过上传文件后,一切正常。但是在测试了字符串之后,服务器无法获取任何信息(键,值等)。

在控制台上,我可以正确打印出表单数据

console.log(Array.from(formData));

结果:

(13) [Array(2),Array(2),Array(2)]
0: (2) ["pnum","123"]
1: (2) ["age","123"]
2: (2) ["gender","male"]
3: (2) ["ethn","Hispanic or Latino"]
4: (2) ["q1","undefined"]
5: (2) ["q2","123"]
6: (2) ["q3","123"]
7: (2) ["q4","123"]
8: (2) ["q5","123"]
9: (2) ["q6","123"]
10: (2) ["q7","123"]
11: (2) ["q8","123"]
12: (2) ["q9","123"]
length: 13
__proto__: Array(0)

但是一旦我在服务器端检查了

console.log(req.files);

它只是返回一个空数组:

[]

如果我将字符串和文件都附加到formdata中,则仅文件将被上传,字符串仍然丢失。

app.js:

routes.post('/',upload.any(),async function(req,res) {
    if (req.files) {
        console.log(req.files);
    }
})

前端js:

console.log(Array.from(formData));
$.ajax({
    url:  window.location.href,data: formData,type: 'POST',contentType: false,processData: false,success: function(success) {
        console.log(success);
    },error: function(error) {
        console.log(error);
    }
});
YC4130332000 回答:formdata只能正确发布文件,但会删除所有字符串。 (使用node,express和multer)

我知道了,formdata中的字符串也返回到req.body而不是req.files

本文链接:https://www.f2er.com/3042233.html

大家都在问