Prealoder when requesting dowload

3

I'm doing a return of a pdf from a Custom ActionResult

response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=teste.pdf");

And I would like to know how I could make a preloader while the server processes this download

[Edit]

As requested, the link code that makes the download request

@Ajax.ActionLink("Print", "Report", new { id = 20 }, new AjaxOptions { 
        Confirm="Are you cure?",
        HttpMethod="GET",
        LoadingElementId="divLoading"
    })
    
asked by anonymous 16.09.2015 / 19:20

1 answer

0

Let's say you already have preloader ready (html, css, js) and need only call and hide it at download time. If you do not have it, here is an example.

You can use ajax to call your ActionResult and call your preloader ao mesmo tempo.

<script>
    $(".downloadAnexo").click(function () {
        $("#divPreloader").fadeIn();//Chama o preloader
        $.ajax({
            url: '@Url.Action("DownloadFile")?id=' + 1,//Sua ActionResult
            type: 'GET',
            success: function () {
                $("#divPreloader").hide();//Esconde o preloader em caso de sucesso
            }, error: function () {
                $("#divPreloader").hide();//Esconde o preloader em caso de erro
            }  
        });
    });
</script>

See an example at dotNetFiddle.

Edit

Using the Ajax.ActionLink you have both the OnBegin and OnComplete options. Just call the functions to show and hide preloader . It would look like this:

@Ajax.ActionLink("Print", "Index", new { id = 20 }, new AjaxOptions
{
    Confirm = "Are you cure?",
    HttpMethod = "GET",
    LoadingElementId = "divLoading",
      OnBegin = "onAjaxBegin",//Chamo a função onAjaxBegin
    OnComplete = "onAjaxComplete"//Quando completar, chamo a função onAjaxComplete
})

And you create the functions to do what you want:

<script>
   function onAjaxBegin() {
            $('#divPreloader').fadeIn();
        }

        function onAjaxComplete() {
            $('#divPreloader').hide();
        }
    </script>
    
17.09.2015 / 16:21