我有以下方法ExportarIntervalos在控制器「MetasComisionesController」如何返回一個Excel文件上按一下按鈕使用jQuery和ASP.NET MVC4下載
[HttpPost]
public ActionResult ExportarIntervalos()
{
var products = new System.Data.DataTable("teste");
products.Columns.Add("col1", typeof(int));
products.Columns.Add("col2", typeof(string));
products.Rows.Add(1, "product 1");
products.Rows.Add(2, "product 2");
products.Rows.Add(3, "product 3");
products.Rows.Add(4, "product 4");
products.Rows.Add(5, "product 5");
products.Rows.Add(6, "product 6");
products.Rows.Add(7, "product 7");
var grid = new GridView();
grid.DataSource = products;
grid.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return View("ExportacionExcel");
}
我已經javascript代碼如下:
function btnExportar() {
var r = confirm("Desea exportar todos los Intervalos de Comision ?");
if (r == true) {
//window.alert("Iniciar Exportacion");
$.post("@Url.Action("ExportarIntervalos")",{},
function (data) {
alert("La EXPORTACION TERMINO");
window.location = "/MetasComisiones/" + "ExportacionExcel";
});
} else {
window.alert("Exportacion CANCELADA");
}
}
這是從一個按鈕onclick事件稱爲
<button type="button" class="btn" name="btnExportar" onclick="btnExportar()">Exportar</button>
如何更改以前的任何組件或邏輯以使按鈕btnExportar返回必須作爲響應的一部分下載的文件?我並不需要重定向到新位置