简体   繁体   中英

How to control page redirection when downloading a file in asp.net?

i am finding a challenge on controlling the page redirection. For demo purpose, i have created sample code.

it's simple aspx Page which contains one button and checkbox. On clicking the button, it will download a text file and enables the checkbox via javascript. On clicking the checkbox, it will redirect to different url.

Problem: In my application, On clicking the button, i would be fetching some data from database and send the details to PDF service (Different webservice) which returns byte array then it downloads a PDF file.This process would take 3 to 4 seconds.

Sometimes user is not waiting to downloading the file. moreover i am enabling the checkbox when click on the button. They click the checkbox which redirects the user to another link.

I want to make sure that user should redirect to different page only PDF download completes. Will it be possible to have control on the redirection?

aspx code:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton id="link" runat="server" OnClick="link_OnClick" ClientIDMode="Static">sample link</asp:LinkButton>
        <asp:CheckBox runat="server" id="chkAgree" OnCheckedChanged="chkAgree_OnCheckedChanged" ClientIDMode="Static" Text="I agree" AutoPostBack="True"/>
    </div>
    </form>
    
    
   <script type="text/javascript">
       window.onload = function() {
       var btn = document.getElementById("link");
       document.getElementById("chkAgree").checked = false;
       
       document.getElementById("chkAgree").setAttribute('disabled', true);
       btn.onclick = function(event) {
           event.preventDefault();
           document.getElementById("chkAgree").removeAttribute('disabled');
       }
       };
   </script> 
</body>
</html>

Code behind file:-

 public partial class WebForm : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        private byte[] GetEmployeeAsByteArray()
        {
            var employee = new { id = 101, name = "steven" };
            using (var m = new MemoryStream())
            {
                using (var writer = new BinaryWriter(m))
                {
                    writer.Write(employee.id);
                    writer.Write(employee.name);
                }
                return m.ToArray();
            }
        }

        protected void chkAgree_OnCheckedChanged(object sender, EventArgs e)
        {
            Response.Redirect("https://www.google.com");
        }

        protected void link_OnClick(object sender, EventArgs e)
        {
            var emp = GetEmployeeAsByteArray();
            Response.ContentType = "text/plain";
            Response.Headers.Add("Content-Disposition", "attachment; filename=\"employee.txt\"");
            Response.BinaryWrite(emp);
            Response.End();
        }
    }

with my understanding u can give a loader or some function which allows to check the box if the process is finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM