0

I want to get a response when the download starts, I'm submitting a form which is an HTML form post and I get a file as return which already Flush in the controller. so there is no response from the server-side. Is there any way to get a response from the browser side to know the download is started or not?

function GenerateReport() {
    debugger;
    ChangeButtonPatchView("Report", "buttonPatch", "DisabledGenerateRpt");
    $('#Action').val('Report');
    $('#btnPrintOrSaveReport').trigger('click');
    setInterval(function () {
        ChangeButtonPatchView("Report", "buttonPatch", "SCurvePercentageComplete");      
    },2000);  
}

in this code, the submit button is triggered. I want to check the browser response in this function

Dhanil Dinesan
  • 575
  • 9
  • 27

2 Answers2

1

There is no way to monitor the download status from Javascript alone if you are triggering a regular download. An extension can use the downloads API (Chrome/FF) to query the progress of a download and a page can do the same using a XMLHttpRequest and the onProgress event, but currently no major browser has a way to query the progress of a download with Javascript alone.

If you can offload the downloading to a server-side script you could however track the download using a unique token.

Fdebijl
  • 887
  • 5
  • 20
  • ohh thats the problem i have already checked may references. I just need a response that the download is started. – Dhanil Dinesan Jun 03 '19 at 11:54
  • @DhanilDinesan No browser that I know of will emit an event that the download has started, sadly. The easiest solution would be to use a server-side download host that can track individual downloads and send the status back to the client. – Fdebijl Jun 03 '19 at 11:58
0

here is my try and it's working fine. in Controller, I have added a cookie

HttpCookie dwnloadResponse = new HttpCookie("dwnloadResponse");
                dwnloadResponse["Complete"] = "true";
                Response.Cookies.Add(dwnloadResponse);

and using this cookie name I do my checking in javascript.

 function GenerateReport() {
    debugger;
    ChangeButtonPatchView("Report", "buttonPatch", "DisabledGenerateRpt");
    $('#Action').val('Report');
    $('#btnPrintOrSaveReport').trigger('click');
    _responseCheckTimer = setInterval(function () {
        debugger;
        if (document.cookie.indexOf('dwnloadResponse') !== -1) {
            ChangeButtonPatchView("Report", "buttonPatch", "SCurvePercentageComplete");
            var d = new Date();
            d.setTime(d.getTime() + 0);
            var expires = "expires=" + d.toUTCString();
            document.cookie = "dwnloadResponse=" + ";" + expires + ";path=/";
            clearTimeout(_responseCheckTimer);
        }      
    },1000);          
}

this will check the cookie is available in the browser. if the cookie is not present it will return a -1 value. Then we will remove the cookie and stops the timer.

Dhanil Dinesan
  • 575
  • 9
  • 27