Meteorjs:如何从响应下载附件到.csv文件?

时间:2016-04-13 08:26:44

标签: ajax csv meteor

我无法从Meteorjs的POST调用返回的响应中保存到.csv文件。

回复的相关标题:

content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8.

content-disposition: attachment;filename=Dashboard.xlsx

content-encoding: gzip

回复中的内容:'\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003\u0002\u0000\u0000\u0000��\u0003\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000'

file.write(response.content);只是把那些胡言乱语告诉了csv。

作为替代方案,我认为我可以产生一个子进程并卷曲。

1 个答案:

答案 0 :(得分:1)

我这样解释你的问题:

  

我想返回可供Excel使用的CSV。

您可以为此创建路线。

Router.route('/yourpath/csv', {
    where: 'server',
    name: 'csvRoute',
    action: function () {
        var filename = 'foo.csv';
        var fileData = '\ufeff'; // Add unicode signature for Excel.
        fileData += "header1;header2;"; // headers

        // headers so that the browser knows what to do.
        var headers = {
            'Content-Type': 'text/csv;charset=utf-8;',
            'Content-Disposition': "attachment; filename=" + filename
        };

        // fill with content.
        for(int i = 0; i < 10; i++)
        {
            fileData += "value1_" + i + ";value2_" + i + ";"
            fileData += "\r\n";
        }
        this.response.writeHead(200, headers);
        return this.response.end(fileData);
    }
});

这将返回一个可以在Excel中打开的CSV。

<a target="_blank" href="{{pathFor 'csvRoute'}}">Download CSV</a>

现在我注意到你说你做了一个POST而不是GET。但理论是一样的。