我需要在HTTP响应中发送一个CSV文件。如何将输出响应设置为CSV格式?
这是行不通的:
Response.ContentType = "application/CSV";
我需要在HTTP响应中发送一个CSV文件。如何将输出响应设置为CSV格式?
这是行不通的:
Response.ContentType = "application/CSV";
当前回答
使用text/csv是最合适的类型。
您还应该考虑向响应中添加Content-Disposition报头。通常,一个文本/csv文件会被Internet Explorer直接加载到Excel的托管实例中。这可能是一个理想的结果,也可能不是。
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
上述操作将导致出现一个文件“另存为”对话框,这可能是你想要的。
其他回答
根据RFC 4180, CSV的MIME类型为text/ CSV。
在ASP.net MVC中,你可以使用FileContentResult和File方法:
public FileContentResult DownloadManifest() {
byte[] csvData = getCsvData();
return File(csvData, "text/csv", "filename.csv");
}
我建议在'myfilename.cvs'前面插入一个'/'字符
Response.AddHeader("Content-Disposition", "attachment;filename=/myfilename.csv");
我希望你能取得更好的成绩。
使用text/csv是最合适的类型。
您还应该考虑向响应中添加Content-Disposition报头。通常,一个文本/csv文件会被Internet Explorer直接加载到Excel的托管实例中。这可能是一个理想的结果,也可能不是。
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
上述操作将导致出现一个文件“另存为”对话框,这可能是你想要的。
就像这样使用
Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.Write(t.ToString());
Response.End();