AngularJS & ASP.NET MVC를 사용하여 다운로드 가능한 Excel 컨텐츠로 내보내는 데 문제가 있습니다. 내 최종 결과는 아무 일도 일어나지 않습니다.Excel로 내보내기
샘플 ASP.NET 컨트롤러 방법 :
[HttpPost]
public ActionResult ExportToExcel(Model form)
{
var gv = new GridView();
gv.DataSource = _service.getSomeStuff(form);
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Stuff.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
var sw = new StringWriter();
var htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
byte[] temp = System.Text.Encoding.UTF8.GetBytes(sw.ToString());
return File(temp, "application/ms-excel");
}
각도 컨트롤러 방법 : ->를 통해 트리거 핸들러 NG는 클릭
function exportToExcel() {
$http.post('/Controller/ExportToExcel/', vm.Model)
.success(function (data) {
})
.error(function (data) {
alerts.error(data);
});
}
보기 : 무엇
<a href="" ng-click="vm.exportToExcel()">click me</a>
어떤 제안 내가 잘못하고있는 것일까?
File을 반환하기 전에'Response.End()'를 호출합니다. 나는 그 문제가 – DLeh
@ user167698 인 원인이 상상해보십시오. Response.ClearContent(); Response.Buffer = true; Response.AddHeader ("content-disposition", "attachment; filename = Stuff.xls"); Response.ContentType = "application/ms-excel"; Response.Charset = ""; var sw = new StringWriter(); var htw = 새 HtmlTextWriter (sw); gv.RenderControl (htw); Response.Output.Write (sw.ToString()); Response.Flush(); Response.End(); ' \t 또는'byte [] temp = System.Text.Encoding.UTF8.GetBytes (sw.ToString()); 반환 파일 (temp, "application/ms-excel");'. 둘 다 사용하지 마십시오. – yW0K5o
아쉽게도 Response.End()를 사용하거나 사용하지 않고 시도했습니다. 결과에는 차이가 없습니다. "ExportToExcel (Model form)"메소드는 표준 양식 제출에서 호출 된 경우 다운로드를 생성하는 것으로 보입니다. – user167698