I have a feature in my program where in i download all the files from a server to my directory and then get the path of those file and name and data after being downloaded and store in to mongo database using mongo grid fs.but I have a problem isn't its already redundant that I download the file and then getting the file data ang storing it to mongo db. Is there a way we can download file in node js without writing it to disk or directory but still we could get the file data? so that I can still store those file in mongo without those file being created on my directory like for example storing it to memory ? and get deleted after all was finished.
The reason I want to implement this is that I no longer need to delete the files being downloaded to my directory after I store those files to mongo.
sample code on downloading file.
var download = function (url, dest, callback) {
request.get(url)
.on('error', function (err) { console.log(err) })
.pipe(fs.createWriteStream(dest))
.on('close', callback);
};
final_list.forEach(function (str) {
var filename = str.split('/').pop();
console.log('Downloading ' + filename);
download(str, filename, function () { console.log('Finished Downloading' + "" + filename) });
});
Code on storing on mongo
var tempfile = dest;
var origname = filename;
var writestream = gfs.createWriteStream({ filename: origname });
console.log('Finished Downloading' + " " + filename);
fs.createReadStream(tempfile)
.on('end', function () {
})
.on('error', function () {
})
.pipe(writestream);