www.webdeveloper.com
Results 1 to 7 of 7

Thread: Download Multiple Files with Asp.net

Hybrid View

  1. #1
    Join Date
    Dec 2004
    Posts
    6

    Download Multiple Files with Asp.net

    Hello guys,

    I need one help. I have written a code for downloading files on a given link. Single file download is working properly. but i want to create a functionality for download all. User can check multiple files and then when he selects download button it should download all the selected files automatically.

    regards,


    My current Code :
    Code:
    Dim strRequest() As String '= Request.QueryString("file") '-- if something was passed to the file querystring
            Dim i As Integer
            strRequest = Split("YServer.txt,PkgClnup.log", ",")
            For i = 0 To UBound(strRequest)
                Response.Write(Server.MapPath(strRequest(i)) & " : " & i)
                If strRequest(i) <> "" Then 'get absolute path of the file
                    Dim path As String = Server.MapPath(strRequest(i)) 'get file object as FileInfo                
                    'Dim file As System.IO.FileInfo = New System.IO.FileInfo(path) '-- if the file exists on the server
    
                    Dim fs As FileStream
                    fs = File.Open(path, FileMode.Open)
                    Dim bytBytes(fs.Length) As Byte
                    fs.Read(bytBytes, 0, fs.Length)
                    fs.Close()
    
                    Response.AddHeader("Content-Disposition", "attachment; filename=" & strRequest(i))
                    'Response.AddHeader("Content-Length", File.Length.ToString())
                    Response.ContentType = "APPLICATION/OCTET-STREAM"
                    Response.Flush()
                    Response.BinaryWrite(bytBytes)
                    Response.Clear()
                    Response.ClearHeaders()
                    Response.ClearContent()
                Else
                    Response.Write("Please provide a file to download.")
                End If
                Response.End() 'if file does not exist
            Next

  2. #2
    Join Date
    Oct 2005
    Posts
    189
    Hi arya009

    I think to download multiple files you should first zip all selected files and then download that single zip file. It is quet reasonable since web page's response is stream based and each request will only return a single response stream.
    I hope this helps,
    Thanx
    Abhishek
    www.handshakeit.com

  3. #3
    Join Date
    Dec 2004
    Posts
    6
    Yes Thats the solution i gave to my client but he wants to download them individually and with a single click

  4. #4
    Join Date
    Jul 2007
    Posts
    1
    hi,
    I am also looking for the same solution. As i can download one file but i need to download multiple files to client. Is there any other way to do so?

  5. #5
    Join Date
    Aug 2007
    Posts
    1

    Solution


  6. #6
    Join Date
    Nov 2008
    Posts
    1
    YES, you can download multiple files.

    Open multiple window with javascript with new name, call your aspx page in that new window.

    suppose you want to download 5 files, you will be opening 5 new window from javascript. and you know how to call javascript from code behind :-)

    /// Codebehind

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

    NOTE : flname = pdf file name ('without .pdf extension')


    /// javascript

    function fnOpen(fl)
    {
    var flnm =fl+'.pdf'
    window.open('OpenPDF.aspx?FILENAME='+ flnm,fl);

    }



    /// OpenPDF.aspx code

    in page load


    Response.Clear()
    Response.ContentType = "Application/PDF"
    Response.AppendHeader("Content-Disposition", "attachment; filename=" & Request.QueryString("FILENAME"))
    Response.WriteFile(Request.QueryString("FILENAME"))
    Response.Flush()
    Response.Close()
    happy coding
    Last edited by dharmeshkubavat; 11-12-2008 at 05:47 PM. Reason: Added code snipped

  7. #7
    Join Date
    Jan 2013
    Posts
    1

    Red face

    Quote Originally Posted by dharmeshkubavat View Post
    YES, you can download multiple files.

    Open multiple window with javascript with new name, call your aspx page in that new window.

    suppose you want to download 5 files, you will be opening 5 new window from javascript. and you know how to call javascript from code behind :-)

    /// Codebehind

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

    NOTE : flname = pdf file name ('without .pdf extension')


    /// javascript

    function fnOpen(fl)
    {
    var flnm =fl+'.pdf'
    window.open('OpenPDF.aspx?FILENAME='+ flnm,fl);

    }



    /// OpenPDF.aspx code

    in page load


    Response.Clear()
    Response.ContentType = "Application/PDF"
    Response.AppendHeader("Content-Disposition", "attachment; filename=" & Request.QueryString("FILENAME"))
    Response.WriteFile(Request.QueryString("FILENAME"))
    Response.Flush()
    Response.Close()
    happy coding
    Thank you so much. It works fine for me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles