ashwnacharya
ashwnacharya

Reputation: 14871

Force download of a file from memory with support for downloading more than once

I am dynamically generating a file from server based on user input. I need to provide a download button which, upon clicking, downloads a file to the user's file system.

Also, the user might click the same button twice, upon which the file should download again.

The dynamic generation of file rules out the HttpResponse.TransmitFile() option, which suports mutliple download.

Almost every other option I have come across needs Response.End() to be invoked, which prevents a second download.

How do I satisfy the 'multiple download" requirement?

Read up on Virtual Path providers, which might enable me to use TransmitFile(), but that looks like an overkill for such a simple requirement.

Upvotes: 1

Views: 519

Answers (2)

ashwnacharya
ashwnacharya

Reputation: 14871

Got the answer from here:

http://www.eggheadcafe.com/tutorials/aspnet/3889fc21-1562-4d1b-89e4-cea5576690b2/refresh-web-pages-after-d.aspx

Basically it uses a client side script to reload the page after a download:

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

Upvotes: 0

Scott Wisniewski
Scott Wisniewski

Reputation: 25061

You could try either of the following:

  1. Re-Generate the file each the user clicks download.
  2. Save the file to disk, and stream it from there.

Upvotes: 1

Related Questions