Created
January 16, 2022 00:10
-
-
Save Sourabh-Semalty/6ebf5e7990acbf983b960d1c1a3648b8 to your computer and use it in GitHub Desktop.
Create busboy for uploading images, docs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 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) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment