logo

Refresh Web Pages After Download Completed

By Vasanthakumar D
Printer Friendly Version
View My Articles
1201 Views
    

You too could earn $100 for writing an article for EggHeadCafe.

I would like to address one common issue here and one way solving the issue....



In webpages, you are using the below code to donwload some files...

        Response.ContentType = "application/vnd.ms-word";
        string slFilename = "NewBusiness.doc";
        Response.AddHeader("content-disposition", "attachment; filename=" + slFilename + "");
        string AllContent = ViewState["Cont"].ToString();
        Response.Write(AllContent);
        Response.End();








As you are using Response.End, this will render the page again. So, the changes done before download the file wont reflect in the browser.
this is the normal behaiour of the browsers...

you can overcome this issue using client side page reload using Javascript or anyother scripting languages. Lets take the below example..




<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    var isReload = false;
    function DownloadFiles()
    {
        if(isReload == true)
        {
            isReload = false;
            window.location.reload();     
        }
        else
        {
            isReload = true;
        }
        window.setTimeout("DownloadFiles()", 2000);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lblMsg"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Download" OnClientClick="DownloadFiles();" OnClick="Button1_Click"/>
    </div>
    </form>
</body>
</html>



you can able to see that I am calling a Javascript function DownloadFiles() which reload the page after 2 seconds of the download button is clicked.   The main problem here is, suppose the page take 5 seconds to download the file?....In that case, after 2 seonds the page will be reloaded. So, you wont get the download.

you may increase the setTimeout's duration by 10000 or even 60000. This leads to a problem, suppose your download finished within 2 seconds. So, the page will reload after 10 seconds or 1 minute. And also, you cannot vary the setTimeout interval dynamically, due to dynamice operations before download the file......

So, how can we overcome this ?...................

here is my approach to solve this issue.....

for this, you need to split the download operations into two parts.

1. Geneartion Download File Content and accomplish pre-download operations (Download button)
2. Download the original file from previouly genrated content. (hidden button)

for you need to have a download button along with a hidden button.

Pre-download things will be handled by Download button and download opeartion will be handled by hidden button

here is the Sample HTML for this




 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    var isReload = false;
    function DownloadFiles()
    {
      document.getElementById("btnHidden").click();
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" ID="lblMsg"></asp:Label>
        <asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="btnDownload_Click"/>
        <asp:Button ID="btnHidden" runat="server" Text="Download" OnClick="btnHidden_Click" style="display:none;"/>
    </div>
    </form>
</body>
</html>


you can see that, there two button named as btnDownload and btnHidden. btnDownload is used for pre-download operations and btnHidden is used to download the file.

Lets consider a scenario, I have a list files in database and allow the users to download the files. So, in web page I will a grid wtih filename and download button in each row. Once use downloaded the file from the page, we need to remove the donwloaded file from download list.

for this sceanrio, in pre-download we need to change the selected flag of the file as downloaded in database and store the content of file in session or view-state.

And in download operation, we can retrive the file content from session or view-state and we can download nomrally.

here is the code for this

Pre-Download

protected void btnDownload_Click(object sender, EventArgs e)
    {
        string AllContent = "test";
        ViewState["Cont"] = AllContent;
        lblMsg.Text = "Completed";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "rel", "DownloadFiles();", true);
    }

you can see that, I am saving the file content in view state, and change the message label lblMsg's content to 'Completed'. So, our download button will work normally and render the page.
And  register startup script inovke the DonwlodFiles() Js, which inturn invoke the hidden button's server side click event as
   function DownloadFiles()
    {
      document.getElementById("btnHidden").click();
    }


and in hidden button server click event, you can retrive the file content from view state and write it into to Response for download.

protected void btnHidden_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/vnd.ms-word";
        string slFilename = "NewBusiness.doc";
        Response.AddHeader("content-disposition", "attachment; filename=" + slFilename + "");
        string AllContent = ViewState["Cont"].ToString();
        Response.Write(AllContent);
        Response.End();
    }


I hope this article will be helpful to overcome this issue..........



Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

Article Discussion: Refresh Web pages after download completed...
Vasanthakumar D posted at Monday, June 15, 2009 6:32 AM
Original Article
 




  $1000    Adam Houldsworth - $210  |  Jonathan VH - $142  |  Kirtan Patel - $140  |  F Cali - $117  |  Huggy Bear - $67  |  more Neado  |  Free Icons  |  Privacy  |   (c) 2010