我正在开发一个用户点击按钮的Web应用程序,并且Jquery AJAX调用将转到NodeJS服务器,后者又从Elastic Search中获取记录。我希望将这些记录写入CSV文件并将此文件发送到AJAX,因为可以在浏览器上下载响应。我怎样才能做到这一点?
答案 0 :(得分: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)
})
答案 1 :(得分: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是bean点击的功能,你可以用你的nodejs下载代码文件替换download_file.php