Reputation: 550
I want to execute two operations in one javaScript function
force download of a csv file
send an ajax request to mark records in table that records were downloaded.
Since both of these operations will present the user with prompts and dialogue boxes, I want to delay execution of the 2nd event until the first event is complete.
When the user opens the page, they are presented with a list of current records. They check records they wish to process. The checked records are then packaged and the user is presented with the download "Save" or "Open" box. when that operation is complete then the ajax request should execute.
My thought was I could detect when "a" was removed from the document. But I'm not sure how I would do that.
Here is my code for the download
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob(exportArray, {type: 'text/csv'}));
a.download = 'jobCostHandOff.csv';
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
Upvotes: 5
Views: 18033
Reputation: 1555
I have implemented such a mechanism in a web application with the following workflow:
OK
and the payload contains the link to the file.It seems pretty similar to your need.
The gist of the solution is this:
src
attribute of an iframe
hidden in the pageQuickly looking through the PHP code of my web app, I see this:
// This cookie is for the jQuery.fileDownload plugin
header('Set-Cookie: fileDownload=true; path=/');
You have the name of the JS code that does the last bullet point item in the code comment :-) Another reason among the myriad of reasons why I write commented code...
HTH.
Upvotes: 2