[英]FormData Defined on Front-End, Undefined on Back-End
I want to upload a picture on change and with formData and I believe it's grabbing the information on the front-end because I don't get undefined, but on my back-end it comes as undefined.我想上传一张关于 change 和 formData 的图片,我相信它会在前端获取信息,因为我没有未定义,但在我的后端它是未定义的。
//THIS IS MY INPUT
<input class="second-picture" type="file" name="filename" accept="image/gif, image/jpeg, image/png">
//DEFINING FORMDATA
var formData = new FormData();
formData.append('file', $('input.second-picture').prop('files')[0])
console.log(formData.get("file"))
// THIS CONSOLE GIVES ME:
File {
name: picture.jpg
lastModified: xxx (date)
size: 541092
type: "image/jpeg"
webkitRelativePath: ""
}
Here's the AJAX call and the back-end 'POST'这是 AJAX 调用和后端“POST”
AJAX CALL -
$.ajax({
type: 'POST',
url: `/api/Upload-second-picture`,
crossDomain: true,
data: formData,
cache: false,
processData: false,
contentType: false,
enctype: 'multipart/form-data',
mimeType: 'multipart/form-data',
beforeSubmit : function() {
},
success : function(responseText, statusText, xhr, $form) {
console.log('responseText', responseText)
}
});
router.post('/Upload-second-picture', function(req, res) {
let upload = multer({ storage: storage }).single('filename')
upload(req, res, function (err) {
let file = req.file
console.log('this is my file', file) // RETURNS UNDEFINED
if(file != undefined) {
if (err) {
return res.send("Error: file not uploaded 2", err)
}
return res.send(fileName)
}
})
})
How can I make the back end grab the information too?我怎样才能让后端也抓取信息?
您将文件放在file
键中,在new FormData
,因此使用 multer 获取相同内容应该是很自然的:
let upload = multer({ storage: storage }).single('file');//instead of filename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.