Prealoder when requesting dowload

Asked

Viewed 49 times

3

I’m making a return of a pdf from a Custom ActionResult

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

And I wonder how I could make one preloader while the server processes this download

[Edit]

As requested, the code of the link I make the download request

@Ajax.ActionLink("Print", "Report", new { id = 20 }, new AjaxOptions { 
        Confirm="Are you cure?",
        HttpMethod="GET",
        LoadingElementId="divLoading"
    })
  • Do you mean something thus? If it is, let me know and I’ll answer it.

  • @Randrade, I don’t understand, this is a css/js preloader

  • I could explain better what the preloader that you refer to then?

  • @Randrade hello, then, the link you sent me is a preloader in css/js...but reading my question, the preloader must have synchronized with the server processing, that is, when it finishes the process and returns the file to download, the preloader "closes"

  • Yeah, I get it. I just wanted to know if the preloader that you referred to was just that. Post as you call the action for download, that tomorrow I post an answer for you, I’m on mobile now.

  • ah yes...that’s exactly what I meant by preloader, so it’s a custom actionresult, it does all the processing and returns the answer that I pasted there

  • You call the action by a normal link, or uses ajax or something like?

Show 2 more comments

1 answer

0

Suppose you already own the preloader ready (html, css, js) and need only call and hide the same at the time of download. If you don’t have, here is an example.

You can use ajax to call his Actionresult and call his 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 on dotNetFiddle.

Edit

Using the Ajax.Actionlink you have two options Onbegin and Oncomplete. Just call the functions to show and hide the preloader. 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>
  • This option is when the server returns an already active file or there is a path to the file

  • And how do you call ActionResult then?

  • I use Ajax.Actionlink

  • Could add code to your question?

  • See if the addition helped you.

  • link does not download.... there is no way

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.