0

代码:

@model System.Data.DataTable

<button type="submit" class="btn btn-primary"  data-dismiss="modal" aria-hidden="true"> Download</button>



[HttpPost]
public void getCSV(DataTable dt)
{
   MemoryStream stream = Export.GetCSV(dt);

   var filename = "ExampleCSV.csv";
   var contenttype = "text/csv";
   Response.Clear();
   Response.ContentType = contenttype;
   Response.AddHeader("content-disposition", "attachment;filename=" + filename);
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.BinaryWrite(stream.ToArray());
   Response.End();
}

我想允许用户在单击按钮时导出 CSV 文件。数据表应传递给方法 getCSV。请有人可以帮助我。谢谢

我不介意使用 Tempdata 来存储数据表,然后在控制器中访问它

4

1 回答 1

0

你想要FileStreamResult

就像是:

[HttpPost]
public FileStreamResult GetCSV(someType param)
{
   var dt = MyGetDt(param);
   MemoryStream stream = Export.GetCSV(dt);

   var filename = "ExampleCSV.csv";
   var contenttype = "text/csv";
   return new FileStreamResult(stream, contenttype)
   {FileDownloadName = filename};
}

希望这可以帮助!

于 2013-11-06T15:52:01.960 回答