search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

Refresh Web Pages After Download Completed


By Vasanthakumar D
Printer Friendly Version
View My Articles
44 Views
    

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..........


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