如何根据请求导出和下载csv文件

时间:2013-11-28 10:54:21

标签: node.js

我正在尝试从nodejs导出csv文件。当我运行脚本时,我的csv文件被创建,但我希望它可以根据请求直接下载。 能帮我找一个解决方案吗?

我写了

var express = require('express');
app = express();
var csvHeaders =`['language','labelType'];
var writer = csv.createCsvFileWriter(occurrencecsv ,{'encoding': 'utf8'});
MongoClient.connect('mongodb://'+host+':27017/' + dbName, function(err, db) {
 if(err) {
 throw err;
} else {

var collection = db.collection(collectionName);

  collection.find().toArray(function(err, results) {
  function insertRecords() {
    if(!results.length) {
      console.log("Csv created");
    } else {


    var record = results.pop();
    var recordArray = [];
    for(var i in csvHeaders) {
    ('undefined' != typeof(record['dwc'][csvHeaders[i]])) ? recordArray.push(record['dwc'][csvHeaders[i]]) : recordArray.push('');                  
   switch(csvHeaders[i]) {                  
    case 1:
    if('undefined' != typeof(record['dwc'][language] && 'undefined' != typeof(record['dwc'][language]))) {
    console.log("language identified",record);
    }
    ('undefined' != typeof(record['dwc'][language])) ? recordArray.push(record['dwc'][language]) : recordArray.push('');

    break;
    }writer.writeRecord(recordArray);
        insertRecords();
}
  }
      insertRecords();

我需要根据请求下载脚本。

0 个答案:

没有答案