我需要在ASP.NET MVC应用程序中创建一个具有.csv文件类型的ActionResult。
我将向我的营销合作伙伴提供“不要打电话”的电子邮件列表,我希望它在文件类型中具有.csv扩展名。然后它将自动在Excel中打开。
http://www.example.com/mailinglist/donotemaillist.csv?password=12334
我已成功完成以下操作,但我想确保这是绝对最佳和推荐的方法。
[ActionName("DoNotEmailList.csv")]
public ContentResult DoNotEmailList(string username, string password)
{
return new ContentResult()
{
Content = Emails.Aggregate((a,b)=>a+Environment.NewLine + b),
ContentType = "text/csv"
};
}
此Action方法将对上述链接作出回应。
我只是想知道是否有任何意外冲突的可能性与任何不同版本的IIS,任何类型的ISAPI过滤器或我现在无法想到的任何其他类型的文件扩展名。
我需要100%肯定,因为我会向外部合作伙伴提供这个,并且不想在以后改变主意。我真的看不出任何问题,但也许有些模糊不清 - 或者更像“MVC”这样做的方式。
答案 0 :(得分:29)
我使用FileContentResult操作也做了类似的事情。
public FileContentResult DoNotEmailList(string username, string password)
{
string csv = Emails.Aggregate((a,b)=>a+Environment.NewLine + b);
byte[] csvBytes = ASCIIEncoding.ASCII.GetBytes( csv );
return File(csvBytes, "text/csv", "DoNotEmailList.csv");
}
它将为您添加内容处置标题。
答案 1 :(得分:21)
我认为你的Response必须包含“Content-Disposition”标题。像这样创建自定义ActionResult:
public class MyCsvResult : ActionResult {
public string Content {
get;
set;
}
public Encoding ContentEncoding {
get;
set;
}
public string Name {
get;
set;
}
public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/csv";
if (ContentEncoding != null) {
response.ContentEncoding = ContentEncoding;
}
var fileName = "file.csv";
if(!String.IsNullOrEmpty(Name)) {
fileName = Name.Contains('.') ? Name : Name + ".csv";
}
response.AddHeader("Content-Disposition",
String.Format("attachment; filename={0}", fileName));
if (Content != null) {
response.Write(Content);
}
}
}
在Action中使用它而不是ContentResult:
return new MyCsvResult {
Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
/* Optional
* , ContentEncoding = ""
* , Name = "DoNotEmailList.csv"
*/
};
答案 2 :(得分:6)
这就是我做类似事情的方式。我将其视为下载:
var disposition = String.Format(
"attachment;filename=\"{0}.csv\"", this.Model.Name);
Response.AddHeader("content-disposition", disposition);
这应该在浏览器中显示为具有给定文件名的文件下载。
但是我想不出你为什么不能工作的原因。
答案 3 :(得分:0)
您接受的答案已经足够好了,但它会在输出内容时将输出的内容保留在内存中。如果它生成的文件相当大怎么办?例如,当您转储SQL表的内容时。您的应用程序可能会耗尽内存。在这种情况下你想要的是使用FileStreamResult。将数据提供到流中的一种方法是使用管道as I described here