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?
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)
})
$(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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.