1

I am generating a excel file by using the Ajax call to my Action in my controller class in my ASP.net MVC application.Its working fine but the problem occures some time when my file is in downloading stage and the ajax call delete it.If there is any way without SetTimeout then please tell me.

Generate Excel File

$.ajax({
url: "@Url.Action("GenerateReport", "ClientAdmin")",
type: "POST",
data: { reportStart: reportStart, reportEnd: reportEnd},
dataType: "json",
traditional: true,
success: function (downloadUrl) {
    //Download excel file
    window.location = "/ClientAdmin/Download?file=" + downloadUrl;
    //Delete excel file
    $.ajax({
        url: "@Url.Action("DeleteReportFile", "ClientAdmin")",
        type: "POST",
        data: { file: downloadUrl },
        dataType: "json",
        success: function (downloadUrl) {
        },
        error: function () {
            AlertShow("Error!", "Oops! An error occured");
        }
    })
},
error: function () {
    AlertShow("Error!", "Oops! An error occured");
    }
})
  • keep the ajax call 2 inside ajax call 1 complete function . cheers – super cool Jun 16 '15 at 06:53
  • sorry @super cool the ajax call 2 is already inside 1st ajax call success..and the problem is with following line because it download the excel file and before its completion the 2nd ajax call fires //Download excel file window.location = "/ClientAdmin/Download?file=" + downloadUrl; – Pargat Singh Jun 16 '15 at 06:56
  • yes i just noticed it . err ! sorry . have you tried keeping in complete ? let us know – super cool Jun 16 '15 at 06:57
  • nopes same results with complete...... :( @super cool – Pargat Singh Jun 16 '15 at 07:08
  • I think you question can be simplified by asking: "Event when browser receives file download" Attempts are made to answer that question in this SO thread: http://stackoverflow.com/a/4168965/1099945 – gyosifov Jun 16 '15 at 13:39
  • Were you able to solve your problem?! – noobed Jun 18 '15 at 08:07

1 Answers1

1

I think you need jQuery "when".

http://api.jquery.com/jQuery.when/

$.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) {
  alert( jqXHR.status ); // Alerts 200
});

this is to force something to be synchronous. (One can find the credited answer here)

EDIT

as I am not sure how to test your case I might suggest another solution. Try handling "file downloaded" event somehow and then trigger the delete function. Here is a possible useful answer and blog.

Community
  • 1
  • 1
noobed
  • 1,329
  • 11
  • 24