提示使用node.js和node-csv-parser(节点模块)弹出的csv文件, [英] Prompt a csv file to download as pop up using node.js and node-csv-parser (node module)

查看:582
本文介绍了提示使用node.js和node-csv-parser(节点模块)弹出的csv文件,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用node.js。在我的一个项目中经历一个要求时,我面临一个问题,我应该能够动态地写一些数据到csv文件,并让它作为一个弹出窗口为用户下载(使用保存和取消选项 - 正如我们通常看到)。在googling了一段时间后,我决定使用csv npm模块 https://github.com/wdavidw/node -csv-parser 。我能够将数据写入文件并使用此模块保存。

Recently I have started working with node.js. While going through a requirement in one of my projects I am facing an issue where I should be able to write some data to a csv file dynamically and let it prompt as a popup to download for user (with save and cancel options - as we normally see). After googling for some time I decided to use csv npm module https://github.com/wdavidw/node-csv-parser. I am able to write data into a file and save it using this module. I want to prompt a popup for saving this file with/without saving the file.

我的代码看起来像这样:

my code looks something like this:

    // Sample Data 
    var data = [["id", "subject1", "subject2", "subject3"], ["jack", 85, 90, 68], ["sam", 77, 89, 69]]

    // Server Side Code    
    var csv = require('../../node_modules/csv');            
    var fs = require('fs');

    createCSV = function(data, callback) {
        csv().from(data).to(fs.createWriteStream('D:/test.csv')) // writing to a file           
    }

    // Client side call sample
    $("#exportToCSV").click(function() {
        callToServer.createCSV(data);
       return false;
    });

这对编写csv文件来说效果不错。

This is working good as far as writing the csv file is concerned.


  • 我想立即提示此文件以供用户下载。


任何帮助都非常感谢。
-Thanks

Any help is greatly appreciated. -Thanks

推荐答案

我做了这样的事情:

http.createServer(function(request, response) {
    response.setHeader('Content-disposition', 'attachment; filename=testing.csv');
    response.writeHead(200, {
        'Content-Type': 'text/csv'
    });

    csv().from(data).to(response)

})
.listen(3000);

这篇关于提示使用node.js和node-csv-parser(节点模块)弹出的csv文件,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆