C#/JS/JQuery - download a file with a loading panel

Easy way to use export a document in C # and maintain the loading functionality.

First add the functions of add / get cookies
https://utilitiesforprogrammers.blogspot.pt/2018/01/jsjquery-cookies.html

Second add the loading panel
https://utilitiesforprogrammers.blogspot.pt/2018/01/jsjquery-loading.html

Now in your event of export add the cookie to track that the process is ended
         protected void btnExport(object sender, EventArgs e)
        {
            try
            {
                byte[] bytes = Export();
                this.Page.Response.AppendCookie(new HttpCookie("fileDownloadToken", "1"));
             
                this.Page.Response.Clear();
                this.Page.Response.ContentType = "application/pdf";
                this.Page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + "Doc.pdf");

                this.Page.Response.OutputStream.Write(bytes, 0, (int)bytes.Length);
                this.Page.Response.Flush();
                this.Page.Response.End();
            }
            catch (Exception ex)
            {
            }
        }

Finally, in JQuery, we added a timer to control the state of the download
     $(document).ready(function () {
            fileDownloadCheckTimer = window.setInterval(function () {
                var cookieValue = getCookie('fileDownloadToken');
                console.log(cookieValue); if (cookieValue == null || cookieValue != "0") {
                    setCookie('fileDownloadToken', '0;path=/'); $(".loading").remove();
                }
            }, 1000);
        });

Comments

Popular posts from this blog

Download file from input type file javascript

POST an array of objects with $.ajax to C# WebMethod

SQL Views - Variables