Uploading files in chunks and reading chunks to return the original files in Nodejs

Deepak Rai
2 min readJul 31, 2021

--

Below is the upload function

exports.uploadFilesChunk = (req, res) => {

var form = new formidable.IncomingForm()

form.parse(req, (err, fields, files) => {

const readStream = fs.createReadStream(files.demo_image.path, { highWaterMark: 30 * 1024 });

let createFileNameToUpload = files.demo_image.name.split(‘.’)

var dir = __dirname + ‘/../uploads/’ + createFileNameToUpload[0]

readStream.setEncoding(‘utf8’);

fileNameToUpload = files.demo_image.name.split(‘.’).join(‘-’ + Date.now() + ‘.’);

var writableStream = fs.createWriteStream(dir + ‘/’ + fileNameToUpload + “.part.gzip”);

if (!fs.existsSync(dir)) {

fs.mkdirSync(dir);

}

readStream.on(‘data’, (chunk) => {

const gzipStream = zlib.createGzip(chunk);

writableStream.write(gzipStream.toString());

});

readStream.pipe(writableStream);

})

}

Note :- So you can do basic check like checking the file size first and then start uploading the files. If file size is less than 2MB or 4 MB then you better upload them directly on the server. For this please import const zlib = require('zlib'); const formidable = require(‘formidable’);

Below is the function to read chunks and return original file

exports.readUploadedChunk = (req, res) => {

var form = new formidable.IncomingForm()

form.parse(req, (err, fields, files) => {

const directoryPath = path.join(__dirname + ‘/../uploads/’ + fields.file_name);

var writableStream = fs.createWriteStream(__dirname + ‘/../uploads/’ + fields.file_name);

fs.readdir(directoryPath, function (err, files) {

if (err) {

return console.log(‘Unable to scan directory: ‘ + err);

}

//listing all files using forEach

const filesDataDir = files.filter(a => a.endsWith(‘.part.gzip’));

filesDataDir.forEach(function (file) {

// Do whatever you want to do with the file

fs.readFile(__dirname + ‘/../uploads/’ + fields.file_name + ‘/’ + file, (err, data) => {

if (err) throw err;

console.log(data);

writableStream.write(data);

});

});

});

})

}

You can test above both functions using postman. Please let me know in-case of any issues/improvements.

--

--