Pargat Singh
Pargat Singh

Reputation: 76

File deleted before downloaded in Ajax call in Asp.net MVC

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");
    }
})

Upvotes: 1

Views: 284

Answers (1)

noobed
noobed

Reputation: 1339

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.

Upvotes: 1

Related Questions