我需要將文本數據導出到MVC3中的csv。 我做到以下幾點:如何使用MVC 3將文本數據導出到csv?
VIEW:
$(".export").click(function() {
$.get("@Url.Action("Export","Log")");
});
控制器:
public ActionResult Export()
{
var sb = new StringBuilder();
var list = this.systemLogRepository.GetFilterList(
null, this.ControllerContext.RequestContext.HttpContext.Request.QueryString, null);
foreach (var item in list)
{
sb.AppendFormat(
"{0},{1},{2},{3},{4}", item.Machine.Name, item.PackageID, item.ErrorDescription, item.OccurenceTime, Environment.NewLine);
}
return this.File(new UTF8Encoding().GetBytes(sb.ToString()), "text/csv", string.Format("Log-{0}.csv", DateTime.Now.ToString("g").Replace("/","-").Replace(":","_").Replace(" ", "-")));
}
這將返回的內容,但不彈出與另存爲,打開選項 一個窗口? 謝謝
你可以在不同的瀏覽器中試用嗎? – gideon