代码:
@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存储数据表,然后在控制器中访问它
答案 0 :(得分:0)
类似的东西:
[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};
}
希望这有帮助!