ASP.NET AJAX File Downloads using IFRAMEs

ASP.NET AJAX File Downloads using IFRAMEs

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
1,081 views

All implemented this functionality in ASP.NET applications at least several times. Any time you’re dealing with report data, it’s expected that the data be available for download. Unfortunately, AJAX makes this somewhat difficult. Since there is no traditional HTTP response, you have no context with which to send the file to the browser for normal download.

Enter inline frames (IFRAME). Probably one of the most under utilized HTML elements around, dynamically creating an IFRAME allows you to round trip an HTTP request and response without disrupting the AJAX-ness of your async postback. Since any browser that supports XmlHttpRequest supports IFRAMEs, it is as safe to use as AJAX is in the first place. This is a simple example of the technique, using a static list of files in a dropdown, but it could be adapted to more dynamic file creation scenarios easily.

This is the example download page. The JavaScript comments explain how the IFRAME is created and directed to the GenerateFile.aspx:

<html>
<body>
<form id="form1" runat="server">
<asp :ScriptManager runat="server" ></asp>
<script language="javascript">
  // Get a PageRequestManager reference.
  var prm = Sys.WebForms.PageRequestManager.getInstance();

  // Hook the _initializeRequest event and add our own handler.
  prm.add_initializeRequest(InitializeRequest);

  function InitializeRequest(sender, args)
  {
    // Check to be sure this async postback is actually
    //   requesting the file download.
    if (sender._postBackSettings.sourceElement.id == "DownloadFile")
    {
      // Create an IFRAME.
      var iframe = document.createElement("iframe");

      // Get the desired region from the dropdown.
      var region = $get("Region").value;

      // Point the IFRAME to GenerateFile, with the
      //   desired region as a querystring argument.
      iframe.src = "GenerateFile.aspx?region=" + region;

      // This makes the IFRAME invisible to the user.
      iframe.style.display = "none";

      // Add the IFRAME to the page.  This will trigger
      //   a request to GenerateFile now.
      document.body.appendChild(iframe);
    }
  }
</script>
<asp :UpdatePanel runat="server">
  <contenttemplate>
    <asp :D ropDownList runat="server" ID="Region">
      </asp><asp :ListItem Value="N">North Region</asp>
      <asp :ListItem Value="W">West Region</asp>
      <asp :ListItem Value="SE">Southeast Region</asp>
    </contenttemplate></asp>
    <asp :Button runat="server" ID="DownloadFile" Text="Generate Report" ></asp>

</form>
</body>
</html>

GenerateFile.aspx can be empty, other than the Page directive to wire up the code file. There, you write the file to the response object just the same as you normally would:

protected void Page_Load(object sender, EventArgs e)
{
  string FileResponse;
  string Region = Request.QueryString["Region"];

  // Code here to fill FileResponse with the
  //  appropriate data based on the selected Region.

  Response.AddHeader("Content-disposition", "attachment; filename=report.csv");
  Response.ContentType = "application/octet-stream";
  Response.Write(FileResponse);
  Response.End();
}

Now, when DownloadFile is clicked the file will be sent to the user asynchronously. Easy as that.

Source: http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/

http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/stumbleupon_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/delicious_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/blinklist_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/furl_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/technorati_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/magnolia_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/google_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/myspace_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/facebook_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/sphinn_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/mixx_32.png http://www.ajaxupdates.com/wp-content/plugins/sociofluid/images/twitter_32.png

Related Listings:

  1. Ajax Style File upload for .Net If you visit Asp.net Ajax Forum, you will find hundreds...
  2. Pageflakes or Netvibe Ajax IGoogle panes IGoogle, PageFlakes, NetVibes. Beloved child wears many names. Gaia was...
  3. Username availability checker – Ajax Script When you have a name as common as mine, you...
  4. File uploads using ajax Here you can test out the AJAX file upload script....
  5. File Tree Panel – Easy Management Files File Tree Panel is client-server application where client (browser) provides...

Do you like this post?

Email:     

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

4 Comments »

  1. avatar comment-top

    [...] See original here: ASP.NET AJAX File Downloads using IFRAMEs [...]

    comment-bottom
  2. avatar comment-top

    Hi,

    Thanks for your posting. It works fine for IE 7. But, for IE 6, it does not open the dialog box where it suppose to show Open, Save and Cancel button.

    Do you have any idea?

    comment-bottom
  3. avatar comment-top

    forget ie6, it’s the worst ever browser from microsoft. move on.

    comment-bottom
  4. avatar comment-top comment-bottom

RSS feed for comments on this post. TrackBack URL

Leave a comment



Web Design & CSS (Templates) - TOP.ORG