Node.js AJAX Post请求图像文件在Express Route返回空文件
我相信网上很少有这样的例子,但是我遇到了一个与我的申请相关的问题。我有一个接受图像的输入标签。然后,使用AJAX POST请求将这些图像发送到路由,这就是问题所在。在我控制台Node.js AJAX Post请求图像文件在Express Route返回空文件,node.js,ajax,express,Node.js,Ajax,Express,我相信网上很少有这样的例子,但是我遇到了一个与我的申请相关的问题。我有一个接受图像的输入标签。然后,使用AJAX POST请求将这些图像发送到路由,这就是问题所在。在我控制台req.body的路径上,它是空的 profile.hbs <div class="wrapper"> <input type="file" id="uploadfile" accept="image/png,image/jpeg,image/jpg" data-max-si
req.body
的路径上,它是空的
profile.hbs
<div class="wrapper">
<input type="file" id="uploadfile" accept="image/png,image/jpeg,image/jpg" data-max-size="51200" />
</div>
index.js
function readURL(input, img){
if (input.files && input.files[0]) {
if(input.files[0].size > 51200){
return false;
}
let reader = new FileReader();
reader.onload = function(e) {
$(img).attr('src', e.target.result).show();
imageRead = true;
};
reader.readAsDataURL(input.files[0]);
let file = input.files[0];
let fd = new FormData();
fd.append("file", file);
$.ajax({
type: 'POST',
url: '/profilePic',
cache: false,
contentType: false,
processData: false,
data: fd,
success: function(data){console.log(data);},
error: function(err){console.log(err);}
});
}
}
$("#uploadfile").click().change(function(){
readURL(this, '#profilePic');
});
router.post('/profilePic', function(req, res, next){
console.log(req.body); //This is returning empty
});
您不能检查
req.body
以获取上传的文件信息,请使用req.file
或req.file.filename
获取上传文件的名称我认为它们不会“出现”在req.body上。尝试req.files我刚刚检查了它,我的应用程序中似乎有req.files,因为我使用第三方模块进行文件上传。我建议你也这么做,非常简单。我正在使用express-fileuploadreq。文件未定义