Last active
September 15, 2024 08:22
-
-
Save rhamedy/8f29ec90a00fcf8fec8f2c82bd34d10e to your computer and use it in GitHub Desktop.
Upload files with busboy module nodejs and expressjs
This file contains 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
//you do not necessary need all of the following, i copy/pasted a piece from | |
//one of my projects. | |
var express = require('express'); | |
var fs = require('fs'); | |
var Busboy = require('busboy'); | |
var mime = require('mime'); | |
var https = require('https'); | |
var querystring = require('querystring'); | |
var router = express.Router(); | |
//route used to upload vehicle photos via browser | |
router.post('/secure/api/upload', function(req, res) { | |
logger.info("ROUTE POST /secure/api/upload"); | |
//in addition to input-upload field, i am expecting an input field too | |
var vehicleId = req.session["vId"]; | |
//I keep track of huge files | |
var hugeFileSize = []; | |
// I keep track of files with wrong mimetypes | |
var invalidFileTypes = []; | |
// 6MB size limit, I abort upload if file is over 6MB limit | |
var busboy = new Busboy({ | |
headers: req.headers, | |
limits: { | |
fileSize: 6*1024*1024 | |
} | |
}); | |
//keep track of when file finishes and saved in the disk, busboy is a tricky module | |
var counter = 0; | |
//keeping track of all files uploaded, so that later they get resided, don't have to apply to | |
//your case | |
var filenames = []; | |
//vehicleId must be the first field in the upload form or there could be issues | |
//with busboy if you want to include other input fields in the upload form, they have to | |
//be before the upload field or you will have issue accessing them | |
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) { | |
//add checks for field names and extract those values, for example I am expecting a | |
//hidden field with name (vehicleId) | |
if(fieldname == 'vehicleId') | |
vehicleId = val; | |
}); | |
//assuming that vehicleId is not null, already processed. | |
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | |
//validate against empty file fields | |
if(filename.length > 0) { | |
// validate file mimetype | |
if(mimetype != 'image/png' && mimetype != 'image/jpeg' && mimetype != 'image/tiff'){ | |
//keeping track of invalid files, N/A | |
invalidFileTypes.push(filename); | |
//Ignore the upload, move on to next one | |
file.resume(); | |
} else { | |
/* | |
* As soon as 6 MB limit is reached, the rest of the data is skipped hence, | |
* uploading 50 GB would never be possible because after 2.1 MB no more data | |
* is read and the stream is saved as is and consequently deleted. | |
* | |
*/ | |
//Just keeps track of file uploads (how many uploaded). | |
counter++; | |
//Just keeps track of names of files uploaded, for later resizing purpose. | |
filenames.push(filename); | |
file.on('limit', function(){ | |
//console.log('file size over 6 MB.'); | |
hugeFileSize.push(filename); | |
//if the file was large in size then decrement the counter as it will be deleted anyways | |
counter--; | |
//delete the file that is large in size | |
fs.unlink(__dirname + '/../photos/' + vehicleId + '/' + filename); | |
}); | |
//storing the uploaded photo | |
fstream = fs.createWriteStream(__dirname + '/../photos/' + vehicleId + '/' + filename); | |
file.pipe(fstream); | |
fstream.on('close', function() { | |
//file saved in disk, so decrement. | |
counter--; | |
}); | |
} | |
} else { | |
file.resume(); | |
} | |
}); | |
busboy.on('finish', function() { | |
//console.info('Busboy uploading finished!'); | |
}); | |
return req.pipe(busboy); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you help me upload video optimize script reference. i am new in nodejs.