简体   繁体   English

如何在Node.js中将Buffer对象转换为文件?

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

I'm developing a web application using (MySQL)EAN stack. 我正在使用(MySQL)EAN堆栈开发Web应用程序。 I saved png/jpg/pdf as BLOB in MySQL. 我在MySQL中将png / jpg / pdf保存为BLOB。 Now, I can retrieve them as Buffer objects in Node.js by using Sequelize.js : 现在,我可以使用Sequelize.js在Node.js 中将它们检索为Buffer对象:

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. 将Buffer对象返回到Angular.js,然后找到一种从它们生成可下载链接的方法。

  • Convert the Buffer objects to ascii by using bufferObject.toString('ascii'), then, find a way to transform these ascii to downloadable links 通过使用bufferObject.toString('ascii')将Buffer对象转换为ascii,然后找到一种将这些ascii转换为可下载链接的方法

I have been trying for a day and still can't figure it out in both approaches :( , any suggestion would be highly appreciated! 我已经尝试了一天,但仍然无法通过两种方法来解决:(,任何建议将不胜感激!

I think #1 is the best option. 我认为#1是最好的选择。 Dumping the Buffer to a file on the file system in a publicly accessible directory is likely the best bet. 将缓冲区转储到可公开访问目录中的文件系统上的文件中可能是最好的选择。

FileSystem Write in Node 文件系统写入节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM