javascript - How to check if a file download is complete in Web API -


i working on task user can download data in excel format. data dynamically generated (as generic list)and written excel file, working perfectly.

problem:

the problem facing is, when data huge, takes several seconds download file. during time, need show kind of progress/ indication end users (let's say, downloading.... message). current code, not able track when file download complete...

here code,

js code passes web api method , search criteria

var request = {}; request.code = codeddl.find(':selected').val();  $.blockui({ message: '<h3>downloading...</h3>' }); //export excel records $().largedownload("api/filedownload/getworkqueuelist", {    'request': json.stringify(request) }); 

js code reads search criteria , pass web api method:

(function (d) {     d.fn.largedownload = function (a, b, c) { void 0 !== c ? (c = c.touppercase(), "get" != c && (c = "post")) : c = "post"; if (void 0 === b || !1 == b) b = d().parse_url(a), = b.url, b = b.params; var e = d("<form></form"); e.attr("method", c); e.attr("action", a); (var f in b) = d("<input />"), a.attr("type", "hidden"), a.attr("name", f), a.attr("value", b[f]), a.appendto(e); d("body").append(e); e.bind("submit", downloadedcallback); e.submit() }; d.fn.parse_url = function (a) {         if (-1 == a.indexof("?")) return { url: a, params: {} }; var b = a.split("?"), = b[0], c = {}, b = b[1].split("&"), e = {}, d; (d in b) {             var g =             b[d].split("="); e[g[0]] = g[1]         } c.url = a; c.params = e; return c     } })(jquery); 

web api

    [httppost]     public httpresponsemessage getworkqueuelist()     {         var resp = new httpresponsemessage(httpstatuscode.ok);         var currentcontext = httpcontext.current;          try         {             string strrequest = currentcontext.request.form["request"];             var serializer = new javascriptserializer();             var request = serializer.deserialize<viewrequest>(strrequest);             request.queryallrecords = true;             var workqueuelist = mappingmanager.getworkqueuelist(request);              var legacyexcelview = //get data generic list             ....             ....              var filebytes = excelmanager.getexceldata<wqexcelview>(legacyexcelview);             var stream = new memorystream(filebytes);             resp.content = new streamcontent(stream);             resp.content.headers.contenttype = new mediatypeheadervalue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");             resp.content.headers.contentdisposition = new contentdispositionheadervalue("attachment") { filename = "workqueuelist.xlsx" };             resp.content.headers.add("content-encoding", "utf-8");             return resp;         }         catch (exception ex)         {                             resp.content = new stringcontent(ex.message);             return resp;         }     } 

what i've tried:

in largedownload javascript function, added downloadedcallback not seem work expect. fired , way before download complete.

question:

how check if file download complete in web api method... should display progress bar until file download dialog displayed?

we typically solve problem 2-3 api calls. our applications written in java; however, same basic principle applies here.

the first call initiate large sql query, large file creation, etc. in separate thread. request returns immediately, id refer original request.

while rows in csv being written in spawned thread, write 2 property values in original thread number of rows processed , total rows. if it's large sql statement batch statement several limited sql statements progress values can set incrementally , displayed, instead of sitting @ 0% while , snapping 100%. breaking sql statements comes @ added cost of milliseconds provides ton of value in terms of user experience.

we have second api call polled , returns property values number processed , total count. use animate our progress bar. when hits 100% or status flag on response of call changes, request finished file in separate api call. return file pointer/path in same progress call.


Comments