angularjs - Downloading excel file using MVC 5 and WebAPI -
here's i'm using: angularjs, breezejs on client side. server, i'm using web api , mvc 5.
i've been searching ways on how build , retrieve excel spreadsheet server, i'm @ loss.
i found plugin called linqtocsv helps in building csv file out of linq. codeproject example found here , nuget link found here.
i found nice useage on question on stackover flow: web api httpresponsemessage content return incomplete
so after implementing above plugin - linq csv, , following mentioned stackover question, nothing seems working.
here server side code (both taken directly link mentioned already):
[httppost] public httpresponsemessage distributorreport(jobject formvalues) { try { var cqmo = formvalues.toobject<distributorcqmo>(); var query = new finddistributorsbylocationquery { states = cqmo.states.select(x => x.id), cities = cqmo.cities.select(x => x.id), citiesexclusion = cqmo.excludedcities.select(x => x.id), zipcodes = cqmo.zipcodes.select(x => x.id), zipcodesexclusion = cqmo.excludedzipcodes.select(x => x.id), productfamily = cqmo.productfamily.id, productsubfamilies = cqmo.productsubfamily.select(x => x.id), matchallfilter = cqmo.matchallfilter, clienttypefilter = cqmo.clienttypefilter }; ienumerable<distributordto> distributors = this.queryprocessor.process(query).asqueryable(); csvfiledescription outputfiledescription = new csvfiledescription { separatorchar = '\t', // tab delimited firstlinehascolumnnames = true }; httpresponsemessage response = new httpresponsemessage(httpstatuscode.ok) { content = new csvcontent<distributordto>(outputfiledescription, "observertriplist.csv", distributors) }; return response; } catch (exception ex) { // logger.errorexception("exception exporting excel file: ", ex); return request.createresponse(httpstatuscode.internalservererror); } } and here's helper class:
public class csvcontent<t> : httpcontent { private readonly memorystream _stream = new memorystream(); public csvcontent(csvfiledescription outputfiledescription, string filename, ienumerable<t> data) { var cc = new csvcontext(); var writer = new streamwriter(_stream); cc.write(data, writer, outputfiledescription); writer.flush(); _stream.position = 0; headers.contenttype = new mediatypeheadervalue("application/octet-stream"); headers.contentdisposition = new contentdispositionheadervalue("attachment"); headers.contentdisposition.filename = filename; } protected override task serializetostreamasync(stream stream, transportcontext context) { return _stream.copytoasync(stream); } protected override bool trycomputelength(out long length) { length = _stream.length; return true; } } i tried using breeze make post, returns data wrapped in breeze object, , if after digging returned object, it's httpreponse property, you'll see in raw form, tabbed delimited data, however, content application in application/json; charset=utf-8 , not of excel formatting.
edit:
i have following working:
$.ajax({ type: 'post', url: 'mappingtool.api/api/report/distributorreport', data: json.stringify(jsondata), contenttype: 'application/json; charset=utf-8', datatype: 'json', success: function (returnvalue) { alert("success") // window.location = '/reports/download?file=' + returnvalue; } }); i'm getting correct response , able view results in fiddler , in chrome debugger. how browser download file automatically?
any feedback appreciated!
edit:
is should fetching mvc controller or api controller? seems there more support mvc controller, fileresult type can used in mvc; give me automatic download: "exporting excel file view mvc" i've seen being done using api controller; isn't point of api controller delegate simple data types, , not media content?
Comments
Post a Comment