mercredi 31 décembre 2014

How to convert a Buffer object to a file in Node.js?


I'm developing a web application using (MySQL)EAN stack. I saved png/jpg/pdf as BLOB in MySQL. Now, I can retrieve them as Buffer objects in Node.js by using Sequelize.js:



exports.loadUploadedFiles = function(req, res) {
db.File.findAll().success(function(files) {
//res.send(files[0].dataValues.data.toString('ascii')); // I have tried to convert a Buffer to ascii and sent it to Angular.js, however, I still don't know how to transform them to a file link.
res.send(files); // How can I return the Buffer objects to Angular.js
}).error(function(err) {
console.log(err);
res.sendStatus(500);
});
};


I have tried two approaches:




  • Return the Buffer objects to Angular.js, then, find a way to generate downloadable links from them.




  • Convert the Buffer objects to ascii by using bufferObject.toString('ascii'), then, find a way to transform these ascii to downloadable links




I have been trying for a day and still can't figure it out in both approaches :( , any suggestion would be highly appreciated!





Aucun commentaire:

Enregistrer un commentaire