I'm developing a web application where user clicks on a button, and a Jquery AJAX call goes to NodeJS server which in turn fetches the records from Elastic Search. I want these records to be written into a CSV file and send this file to AJAX as response being available for download on browser. How can I achieve this?
Asked
Active
Viewed 874 times
2 Answers
0
var fs = require('fs');
var stream = fs.createReadStream(pathToFile);
stream.on('error', function(){
console.log('404 -------- ', pathToFile);
});
stream.pipe(res);
var destroy = require('destroy'); // nam i destroy --save
var onFinished = require('on-finished'); // nam i on-finished --save
onFinished(res, function (err) {
destroy(stream)
})

MeTe-30
- 2,512
- 2
- 21
- 30
0
$(document).ready(function() {
$(".export").on('click',function(){
var project_id="<?php echo $_GET['project_id'];?>";
$.ajax({
type:"GET",
url: 'https://work.readycontacts.com/True_Validate/download_file.php',
data: "project_id=" + project_id,
success:function(data){
console.log(data);
}
});
return false;
});
});
export is the function that is bean click and you can replace download_file.php with your nodejs download code file

Snehal Gongle
- 337
- 3
- 16
-
then to download the file using nodejs refer this http://stackoverflow.com/questions/17558126/ask-user-where-to-save-file-with-node-js and this http://stackoverflow.com/questions/16847416/download-dialog-not-displaying – Snehal Gongle Jan 24 '17 at 11:10