Ajax Meteorjs:如何将附件从响应下载到.csv文件?
从Meteorjs中的POST调用返回的响应保存到.csv文件时遇到问题 响应中的相关标题:Ajax Meteorjs:如何将附件从响应下载到.csv文件?,ajax,csv,meteor,Ajax,Csv,Meteor,从Meteorjs中的POST调用返回的响应保存到.csv文件时遇到问题 响应中的相关标题: 内容类型:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;字符集=UTF-8。 内容配置:附件;filename=Dashboard.xlsx 内容编码:gzip 响应的内容:”\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0003\u0002\u0000\u0
内容类型:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;字符集=UTF-8。
内容配置:附件;filename=Dashboard.xlsx
内容编码:gzip
响应的内容:”\u001f�\b\u0000\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
作为替代方案,我认为我可以生成一个子进程并进行卷曲。我将您的问题解释为:
我想返回一个可由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);
}
});
Router.route(“/yourpath/csv”{
其中:'服务器',
名称:“csvRoute”,
行动:功能(){
var filename='foo.csv';
var fileData='\ufeff';//为Excel添加unicode签名。
fileData+=“header1;header2;”;;//头
//标题,以便浏览器知道该做什么。
变量头={
“内容类型”:“文本/csv;字符集=utf-8;”,
“内容处置”:“附件;文件名=“+filename
};
//充满内容。
对于(int i=0;i<10;i++)
{
fileData+=“值1+i+”值2+i+“;”
fileData+=“\r\n”;
}
this.response.writeHead(200,headers);
返回this.response.end(fileData);
}
});
这将返回一个可以在Excel中打开的CSV
<a target="_blank" href="{{pathFor 'csvRoute'}}">Download CSV</a>
现在我注意到你说你写了一篇文章而不是一篇文章。但原理是一样的。对不起,我应该再澄清一点:响应来自外部服务器,我想将响应内容存储到.csv文件中。我的错。很抱歉那么我的回答对你没有帮助。祝你好运!您得到的文件似乎是.xlsx
格式,而不是.csv
,您确定请求中没有缺少格式定义的某些参数吗?