-
-
Save shobhitg/5b367f01b6daf46a0287 to your computer and use it in GitHub Desktop.
var path = require('path'); | |
var fs = require('fs'); | |
var os = require('os'); | |
var express = require('express'); | |
var app = express(); | |
var Busboy = require('busboy'); | |
app.get('/', function (req, res) { | |
res.send('<html><head></head><body>\ | |
<form method="POST" enctype="multipart/form-data">\ | |
<input type="text" name="textfield"><br />\ | |
<input type="file" name="filefield"><br />\ | |
<input type="submit">\ | |
</form>\ | |
</body></html>'); | |
res.end(); | |
}); | |
// accept POST request on the homepage | |
app.post('/', function (req, res) { | |
var busboy = new Busboy({ headers: req.headers }); | |
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | |
var saveTo = path.join('.', filename); | |
console.log('Uploading: ' + saveTo); | |
file.pipe(fs.createWriteStream(saveTo)); | |
}); | |
busboy.on('finish', function() { | |
console.log('Upload complete'); | |
res.writeHead(200, { 'Connection': 'close' }); | |
res.end("That's all folks!"); | |
}); | |
return req.pipe(busboy); | |
}); | |
var server = app.listen(3000, function () { | |
var host = server.address().address | |
var port = server.address().port | |
console.log('Example app listening at http://%s:%s', host, port) | |
}); |
The finish event of busboy may be triggered before the writestream has ended, so the "console.log('Upload complete');" is wrong.
Hello ,
Thank you for share this code to public. Can you please add one more example of upload a multiple files ? I need the array of buffer and then I have to upload files in aws s3. After that I have to also save the images name in mongoDB also. Thanks in advanced. :) :) :)
you can upload multiple file with the help of multer
LOl, although I have no idea what Busboy is and when and why I wrote this code. I guess I must be responding to some question on StackOverflow.
I tried your code and I'm getting this error
TypeError: Busboy is not a constructor
@amindadgar I'm getting the same error, any updates?
I am getting same error as well
TypeError: Busboy is not a constructor
Same error for me: Busboy is not a constructor
Figured this out. Busboy is not a constructor - perhaps the code has changed since this example was created. To get this to function, you have to modify as:
const busboyCons = require('busboy');
...
var busboy = busboyCons({ headers: req.headers });
must be faith that this was posted so recently! thanks this helped me a ton :) i am working on a firebase project right now using busboy with a similar code. when my files upload, instead of the .png format, i get a .Object object extension instead. Anyone have any idea what I'm doing wrong?
the busboy package had a major version update earlier this year. I was having issues with +v1. I reverted to v0.4 and it's now working
don't use new keyword while creating busboy instance
wrong:
const bb=new busboy({headers:req.headers})
right:
const bb=busboy({headers:req.headers})
According to current version
I updated to "connect-busboy": "0.0.3"
and it's worked for me.
Hello ,
Thank you for share this code to public. Can you please add one more example of upload a multiple files ? I need the array of buffer and then I have to upload files in aws s3. After that I have to also save the images name in mongoDB also. Thanks in advanced. :) :) :)