How to display and remove a loading .gif when downloading a file?

后端 未结 4 915
孤城傲影
孤城傲影 2021-01-20 01:20

I have a button that, when clicked on, needs to download a file by submitting a form using POST. [Edit to clarify: The file being downloaded is dynamically

4条回答
  •  时光取名叫无心
    2021-01-20 01:59

    I've done this with 2 librairies Under MIT licence : BinaryTransport and FileSaver. In this exemple, i'm downloading an Excel file generated server side.

    HTML (in head tag):

    
    
    

    Javascript :

    function ExportToExcel(urlExcel, fileName){
          $("body").css("cursor", "progress");
          $.ajax({
              url: urlExcel,
              type: "GET",
              dataType: 'binary',
              headers:{'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','X-Requested-With':'XMLHttpRequest'},
              processData: false,
              success: function(blob){
                    $("body").css("cursor", "default");
                    saveAs(blob, fileName);
              },
                error : function(result, status, error){
                    $("body").css("cursor", "default");
              } 
            });  
    }
    

    For a custom loading animation, just call it where I change cursor type at these lines :

    $("body").css("cursor", "progress");
    $("body").css("cursor", "default");
    

提交回复
热议问题