admin管理员组文章数量:1291639
I've been trying to upload a file with node.js and ajax, yet i can't let it work. I don't get an error, but nothing is in my storage folder.
index.js:
app.post('/newFlavour', function(req, res){
console.log("[INFO] New flavour request: " + req.body.name);
let form = new formidable.IningForm();
form.uploadDir = path.join(__dirname, '/storage');
form.on('file', function(field, file) {
fs.rename(file.path, path.join(form.uploadDir, file.name));
});
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
form.on('end', function() {
console.log('success');
});
form.parse(req); });
My function with the ajax request:
function createFlavour() {
let file = $('#upload-input').get(0).files;
const formData = new FormData();
formData.append('uploads', file, file.name);
console.log(file)
$.ajax({
url: "http://localhost:3000/newFlavour",
type: "POST",
dataType: "json",
data: JSON.stringify({
name: $('#name').val(),
file: file
}),
contentType: "application/json",
success: function (data) {
console.log(data);
},
error: function () {
alert("Error occured");
},
plete: function () {
console.log("plete")
}
});
clearModal(); }
The input tag:
<input id="upload-input" type="file" name="upload"></br>
I don't know what i am missing here.
Thanks in advance!
I've been trying to upload a file with node.js and ajax, yet i can't let it work. I don't get an error, but nothing is in my storage folder.
index.js:
app.post('/newFlavour', function(req, res){
console.log("[INFO] New flavour request: " + req.body.name);
let form = new formidable.IningForm();
form.uploadDir = path.join(__dirname, '/storage');
form.on('file', function(field, file) {
fs.rename(file.path, path.join(form.uploadDir, file.name));
});
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
form.on('end', function() {
console.log('success');
});
form.parse(req); });
My function with the ajax request:
function createFlavour() {
let file = $('#upload-input').get(0).files;
const formData = new FormData();
formData.append('uploads', file, file.name);
console.log(file)
$.ajax({
url: "http://localhost:3000/newFlavour",
type: "POST",
dataType: "json",
data: JSON.stringify({
name: $('#name').val(),
file: file
}),
contentType: "application/json",
success: function (data) {
console.log(data);
},
error: function () {
alert("Error occured");
},
plete: function () {
console.log("plete")
}
});
clearModal(); }
The input tag:
<input id="upload-input" type="file" name="upload"></br>
I don't know what i am missing here.
Thanks in advance!
Share Improve this question asked Dec 19, 2017 at 15:50 user6361185user6361185 1-
1
I don't get an error
, or maybe you do.. Your silently ignoring any errors in 2 places I can see,.. one on yourfs.rename
, and again on theform.parse
,.. get the error handling sorted and it might give you a clue to the problem. – Keith Commented Dec 19, 2017 at 15:55
1 Answer
Reset to default 7You should parse your req
at the start. Also per formidable documentation
fileBegin
Emitted whenever a new file is detected in the upload stream. Use this event if you want to stream the file to somewhere else while buffering the upload on the file system.
form.on('fileBegin', function(name, file) { });
so doing this should save your file
app.post('/newFlavour', function (req, res){
var form = new formidable.IningForm();
form.parse(req);
form.on('fileBegin', function (name, file){
file.path = __dirname + '/storage/' + file.name;
});
form.on('file', function (name, file){
console.log('Uploaded ' + file.name);
});
res.status(200);
});
Also you ajax Post is wrong, this should work
var data = new FormData();
$.each($('#upload-input')[0].files, function(i, file) {
data.append('file-'+i, file);
});
$.ajax({
url: 'http://localhost:3000/newFlavour',
data: data,
cache: false,
contentType: false,
processData: false,
method: 'POST',
type: 'POST', // For jQuery < 1.9
success: function(data){
alert(data);
}
});
本文标签: javascriptNodejsajaxuploading a fileStack Overflow
版权声明:本文标题:javascript - Node.js + ajax, uploading a file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741513357a2382737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论