admin管理员组文章数量:1391991
I don't get any errors. The folder uploads
has chmod 777
.
Backend:
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({ storage: storage,
limits: { fileSize: '50mb' }}).single('photo');
router.post('/bild',function(req,res){
console.log("REQ",req); //file is there in the body
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
}
res.end("File is uploaded");
});
});
Frontend:
$("#formular").submit(function (e) {
e.preventDefault();
var form = $(this)[0];
var formData = new FormData(form);
console.log(formData)
$.ajax({
type: "POST",
url: "/users/bild",
data: formData,
processData: false,
"content-type": "application/x-www-form-urlencoded",
success: function(r){
console.log("result",r)
},
error: function (e) {
console.log("some error", e);
}
});
});
But no files were uploaded. I also tried to get the get the file and append it to formData before sending - same effect.
I don't get any errors. The folder uploads
has chmod 777
.
Backend:
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({ storage: storage,
limits: { fileSize: '50mb' }}).single('photo');
router.post('/bild',function(req,res){
console.log("REQ",req); //file is there in the body
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
}
res.end("File is uploaded");
});
});
Frontend:
$("#formular").submit(function (e) {
e.preventDefault();
var form = $(this)[0];
var formData = new FormData(form);
console.log(formData)
$.ajax({
type: "POST",
url: "/users/bild",
data: formData,
processData: false,
"content-type": "application/x-www-form-urlencoded",
success: function(r){
console.log("result",r)
},
error: function (e) {
console.log("some error", e);
}
});
});
But no files were uploaded. I also tried to get the get the file and append it to formData before sending - same effect.
Share Improve this question asked Jul 16, 2017 at 17:37 SuisseSuisse 3,6336 gold badges40 silver badges72 bronze badges 1- this here will save your time stackoverflow./questions/11071100/… – Jerubaal Xerxes Commented Sep 24, 2022 at 5:39
1 Answer
Reset to default 5For the front end, contentType
must be set to false to use a formdata object in jQuery.ajax, also $(this)[0] === this
$("#formular").submit(function (e) {
e.preventDefault();
var formData = new FormData(this);
console.log(formData)
$.ajax({
type: "POST",
url: "/users/bild",
data: formData,
processData: false,
contentType: false,
success: function(r){
console.log("result",r)
},
error: function (e) {
console.log("some error", e);
}
});
});
本文标签: javascriptsimple multipart file upload with expressjs and multer with ajaxStack Overflow
版权声明:本文标题:javascript - simple multipart file upload with express.js and multer with ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744632748a2616639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论