Multiple file download from a single ASP.NET page

Had an application that needed to generate two CSV files to send to corporate. One file was a simple one line header file and the other file had the data details. I really didn't want to have two buttons on my page and make the user create each file independently. While not that big of a deal, wouldn't it be better from the user's perspective if they could create both files from a single click? I thought so. I found some articles detailing multi-part MIME messages that would allow you to stream multiple files out. The premise sounded good, stream each file and include a boundary delimiter to separate files. While the concept was good, the implementation turned out to be impossible for me so I turned to other avenues and stumbled upon a fairly simple way to get it done. Not the most elegant solution but good enough for the controlled environment of a simple internal web application.

On the page that solicits the user input required in order to create the two download files, I have the following javascript function:

 <script language="javascript" type="text/javascript">

   function fnOpen(fl) {      window.open('TBFile.aspx?myId=' + fl);

   }

</script>

On the click event of the "create files" button, I validate the form input and if all is good, do the following to call the javascript function twice (two files so two calls):

Dim myId As String

myId = "HEADER"

ClientScript.RegisterStartupScript(Me.GetType(), myId, "<script language='javascript'>fnOpen('" & myId & "');</script>")

myId = "DATA"

ClientScript.RegisterStartupScript(Me.GetType(), myId, "<script language='javascript'>fnOpen('" & myId & "');</script>")

 

On the page (TBFile.aspx in this example) that the javascript function calls, I simply check the request query string value and write out the proper contents (either Header or Data):

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   '**********************************

   '* Verify query string was passed *

   '**********************************

   If IsNothing(Request.QueryString("myId")) = False Then

      "If Request.QueryString("myId") = "HEADER" Then

        '***************

         '* HEADER file *

         '***************

      ElseIf Request.QueryString("myId") = "DATA" Then

         '*************

         '* DATA file *

         '*************

      End If

   End If

End Sub

Simple and easy. Browser pop-up blockers may prevent the download windows from opening so that is one gotcha you need to be aware of.

 

 

Published Wednesday, January 21, 2009 9:57 AM by bunbun
Filed under: ,

Comments

# re: Multiple file download from a single ASP.NET page

Friday, September 04, 2009 6:13 AM by Arun

ok...

good....

but when i use this code means each and every time the popup blocker is asked..

Leave a Comment

(required) 
(required) 
(optional)
(required)