我在PostgreSQL数据库中有一个以二进制格式(bytea)存储的jrmxl(Jasper报表)文件。我正在尝试读取该文件并将其转换为普通的jrmxl(XML)文件并保存到磁盘上。
以下是我目前尝试过的方法:
如果我运行上面的代码,文件会被保存但是变成了二进制格式。如何将其转换为文本格式的普通xml文件,以便可以在ireport中导入?
以下是我目前尝试过的方法:
var fs = require('fs');
exports.saveFile = function (pg) {
//pg is the postgres connection to query the db
pg.query('Select data from data_file where id = 123', function (err, result) {
if (err) {
console.log(err);
return;
}
var data = result.rows[0].data;
//Buffer.isBuffer(data) === true
// I can get the data here. Now I try to convert it into text
var file = data.toString('utf8');
fs.writeFile('report.jrxml',file, function (er) {
if (er) {
console.log('an error occurred while saving the file');
return;
}
console.log('file saved');
}}
});
}
如果我运行上面的代码,文件会被保存但是变成了二进制格式。如何将其转换为文本格式的普通xml文件,以便可以在ireport中导入?
fs.writeFile('路径', 文件, {encoding: 'utf8'}, function...)
- ndugger