Node - CSV is not working - node.js

I am using nodejs v0.10.26 and expressjs.
I want to implement data export in CSV format functionality in application and for this i am using node-csv 0.3.7
JS
var csv = require('csv');
res.setHeader('Content-disposition', 'attachment; filename=Report.csv');
res.writeHead(200, {'Content-Type': 'text/csv'});
csv().from(callBack).to(res);
But it is not prompting any CSV file, i am getting just text data in service response.
Can anyone tell me what is wrong in my code? I want CSV file prompt in this scenario.
Updates
Here callBack = JSON object which contains data
res = response

Try this:
var csv = require('csv');
res.setHeader('Content-disposition', 'attachment; filename=Report.csv');
res.writeHead(200, {'Content-Type': 'text/csv'});
csv.parse(callBack, function(err, output) {
if(err){
throw err;
}
res.write(output);
});
I tested the csv.parse function with this code:
var parse = require('csv-parse');
var data = '"1","2","3","4"\n"a","b","c","d"';
parse(data, function(err, output) {
if(err){
throw err;
}
console.log(output);
});
Also, as a side note, I'd recommend not calling a data-containing variable callBack, as convention tends to name callback functions callback. It will work your way, but it will confuse anyone reading your code.

Related

Download large csv file using json2csv in node js

i am using json2csv package for downloading my data it is working fine for small data. but if the records are more then 300 values it gets crashed. here is my code
const csvString = json2csv.parse(responseData);
res.setHeader('Content-disposition', 'attachment; filename=shifts-report.csv');
res.set('Content-Type', 'text/csv');
res.status(200).send(csvString);
this code is working perfectly fine on small data how can i stream data when there is large amount of data using the same approach that i followed.
i am trying something like this but it gives me an error that cannot set the headers.
const headers = {
'Content-type': 'text/csv',
'Transfer-Encoding': 'chunked',
'Content-Disposition': 'attachment; filename="file.csv"'
};
res.writeHead(200, headers);
res.flushHeaders();
const stream = new Writable({
write(chunk, encoding, callback) {
res.write(chunk);
callback();
}
});
try {
stream.write(file, 'utf-8');
} catch (error) {
console.log('error', error);
}
}
res.end();
You should use Json2CSV stream instead.
npm install json2csv-stream
const fs = require('fs');
const MyStream = require('json2csv-stream');
// create the one-time-use transform stream
const parser = new MyStream();
// create the read and write streams
const reader = fs.createReadStream('data.json');
const writer = fs.createWriteStream('out.csv');
//You can use writer to write it to the FS
// or can stream the content to the response object
// however, you need to write this code in any route handler
// which is sending the CSV data back to the user
reader.pipe(parser).pipe(res);
//reader.pipe(parser).pipe(writer);
For more details check here.

Dealing with meta data in client request when receiving images

I am facing issues when trying to process an uploaded picture and write it into a file. Usually this work very well that way:
req.on('data', function (data) {
body += data;
wstream.write(data);
});
// end process data
req.on('end', function () {
wstream.end();
});
In that case written file header looks like that:
PNG ... followed by binary data.
But in some cases written file looks like that:
--72e245e8-38eb-41e8-9118-fc0405e4837c Content-Type: multipart/form-data Content-Disposition: form-data; name=image;
filename=picture.jpg; filename*=utf-8''picture.jpg
As you can imagine, those pictures arent working well anymore until I remove that meta data as content-type etc.
But after I do so, picture is fully functional and useable.
I tried to access the request data and called toString Method to replace the "unwanted" parts, but than I entirely mess up content encoding of that output file and it becomes unuseable at all.
data = data.toString(/---.*/g), "");
Any ideas how to do the trick?
I solved my issue by help of module formidable.
var formidable = require('formidable');
var util = require('util');
form.parse(req, function(err, fields, files) {
logger.debug("Received upload: " + util.inspect({fields: fields, files: files}));
});
form.on('fileBegin', function (name, file){
file.path = "MyPathToDestinationFile";
logger.debug("File upload started for file '" + file.path + "'");
});
form.on('end', function() {
logger.debug("File upload finished for file");
// Send response to client
});
form.on('error', function(err) {
logger.debug("Failed to finish upload due to '" + err + "'");
// Send error to client
});

Upload file to servlet from node without saving it

On my node express server, I am receiving a pdf file. I am using the below code to get the pdf contents from the request
var data = new Buffer('');
request.on('data', function (chunk) {
data = Buffer.concat([data, chunk]);
});
request.on('end', function() {
console.log('PDF data is '+JSON.stringify(data));
});
Now that PDF content is available on node, I need to send it as it is to a J2EE server. In order to do that, I am first saving the PDF file in the node server, reading it from the node server and then piping it to request.post (https://github.com/request/request)
var req = require('request');
fs.writeFile('abc.pdf', data, 'binary', function(err) {
if (err) {
console.log('Error ' + JSON.stringify(err) );
throw err;
}
var source = fs.createReadStream('abc.pdf');
//send our data via POST request
source.pipe(req.post('http://'+j2ee_host+':'+j2ee_port+'/myjavaapp/Upload')
});
This works fine. However, I feel the part of saving the PDF file on the node server and then reading it is (before posting to the J2EE server using request module) is completely unnecessary, as I am not making any changes to the file.
Once I have the PDF contents in 'data' variable, I would like to directly post them to the J2EE server. However, I have not been able to find a way to use the request module to directly post file contents. I have seen some examples related to POST using request module but they refer to formData. In my case, I don't have formData but instead reading the file from request and directly posting it to the J2EE server.
Is there a way to achieve this and avoid the file write and read?
EDIT
Below is my complete code
function upload(request, response) {
var data = new Buffer('');
request.on('data', function (chunk) {
data = Buffer.concat([data, chunk]);
});
request.on('end', function () {
fs.writeFile('abc.pdf', data, 'binary', function(err){
if (err) {
console.log('Error ' + JSON.stringify(err) );
throw err;
}
var source = fs.createReadStream('abc.pdf');
source.pipe(req.post('http://'+j2ee_host+':'+j2ee_port+'/myj2eeapp/Upload'));
})
})
}
You can pipe directly from the data request to the servlet
var req = require('request');
function upload(request, response) {
var target = req.post('http://'+j2ee_host+':'+j2ee_port+'/myjavaapp/Upload');
request.pipe(target);
target.on('finish', function () {
console.log('All done!');
//send the response or make a completed callback here...
});
}

Unable to get png file saved if I tried to process the image immediately

I am trying to fetch the image remote then process it by node-tesseract. Code following:
var request = require('request');
var fs = require('fs');
request.get('http://cn.bing.com/s/a/hpc18.png').pipe(fs.createWriteStream('bing.png'));
Code above is doing well and the png file will be saved correctly.
Then I want to process the png by tesseract ocr(node binding)
tesseract.process('bing.png', options, function (err, text) {
//do something
});
After running all code above, I found that the text is null. Then i checked the picture, the png file didn't generate correctly - it's an empty file.
Anyone could help? I tried to sleep some time between those two parts but it didn't work. Why even the png file wasn't generated?
Regards,
Lyu
Can you try this out:
var fs = require('fs');
var request = require('request');
var tesseract = require('tesseract');
request.get({url: 'http://cn.bing.com/s/a/hpc18.png', encoding: 'binary'}, function (err, response, body) {
fs.writeFile("bing.png", body, 'binary', function(err) {
if(err)
console.log(err);
else
tesseract.process('bing.png', options, function (err, text) {
//do something
});
});
});

Writing base64 image data toimage file using sails js

In the current project I am working with sails js as back end and angular js as front end. I have implemented a cropping module. By current problem is that, the output of the cropping module is base64 data. For doing some manipulations I need to convert this base 64 data to an image file. I received the base64 data at server side. Now need to convert this data to a file in sails js server side. I used a code for that, but not creating the image file.
My sample base 64 data is below
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAgAElEQVR4Xuy9B6wkeX4e9lVVd1Xn3P1yDpNz2J3N+e72juFODHfH......................."
my code for converting this base64 to image file is
var image = req.body.image.replace(/^data:image\/jpeg;base64,/, "");
var filePath = 'imagecontents/';
fileName = filePath +'abc.jpg';
mkdirp(path.join(__dirname, '../../..' + filePath), function (err) {
if (err) {
logger.log('error', err);
res.sendStatus(500);
throw err;
}
require("fs").writeFile(path.join(__dirname, '../../..' + fileName), image, 'base64', function (err) {
if (err) {
logger.log('error', err);
res.sendStatus(500);
throw err;
}
});
Plz help to rectify is there is any error in in this code
You can write the file using the below code:
var base64Data = data.replace(/^data:image\/png;base64,/, "");
require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
console.log(err);
});

Resources