Wednesday, 15 February 2012

c# - Download excel file from page via WebApi call -



c# - Download excel file from page via WebApi call -

i'm trying send 9mb .xls file response web api controller method. user click button on page , trigger download via browser.

here's i've got far doesn't work doesn't throw exceptions either.

[acceptverbs("get")] public httpresponsemessage exportxls() { seek { byte[] exceldata = m_toolsservice.exporttoexcelfile(); httpresponsemessage result = new httpresponsemessage(httpstatuscode.ok); var stream = new memorystream(exceldata); result.content = new streamcontent(stream); result.content.headers.contenttype = new mediatypeheadervalue("application/octet-stream"); result.content.headers.contentdisposition = new contentdispositionheadervalue("attachment") { filename = "data.xls" }; homecoming result; } grab (exception ex) { m_logger.errorexception("exception exporting excel file: ", ex); homecoming request.createresponse(httpstatuscode.internalservererror); } }

here coffeescript/javascript jquery ajax phone call button click in interface.

$.ajax( url: route datatype: 'json' type: 'get' success: successcallback error: errorcallback )

now think perhaps datatype wrong , shouldn't json...

i had create couple of little changes work

first: alter method post

[acceptverbs("post")]

second: alter using jquery ajax lib utilize hidden form, here's service function doing hidden form , submitting it.

exportexcel: (successcallback) => if $('#hidden-excel-form').length < 1 $('<form>').attr( method: 'post', id: 'hidden-excel-form', action: 'api/tools/exportxls' ).appendto('body'); $('#hidden-excel-form').bind("submit", successcallback) $('#hidden-excel-form').submit()

hopefully there's improve way time beingness it's working , downloading excel file nicely.

c# asp.net excel asp.net-web-api xls

No comments:

Post a Comment