Results 1 to 7 of 7

Thread: Downloading multiple files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    Downloading multiple files

    Hi Experts,

    I was asked to develop a simple file downloading utility from the server to the client's side. But all that I had came up is downloading of file one by one.

    This is the scenario:

    I had listed the files to be downloaded in a listbox and the user will select files to download. However, the user must have a choice to download all files or selecting mutiple files. It's too tiresome for the user to download one by one file that he needed. My application displays a dialog box for the user to save the file.

    Below is how I call the function to download multiple selected files.


    VB Code:
    1. Dim item As ListItem
    2.  
    3.         For Each item In ListBox1.Items
    4.  
    5.             If item.Selected Then
    6.  
    7.                 DownloadFile(Session("userdir") & item.Text, False)
    8.  
    9.             End If
    10.  
    11.         Next



    But after downloading the first file, it's not asking for the next selected file.

    I didn't want to download files at the same time. What I wanted is to download file right after the other.

    Any help will be greatly appreciated.

    Thanks in advance.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Ok, I know your using VB.Net, but if you would like, I have a download class I built in C#. All you do is pass in an string array of links to download and the path to save them to, then call the startdownload method.

    Each time a download completes, an event is raised telling you which one was just downloaded, the total amount it is downloading, and how many have been downloaded so far.

    It works well for me.

    Here is the dll. Just add a reference to it in your project, and you can create an instance of it.

    I also suggest that you call the StartDownload method in its own thread that way your app won't lock up. You will know when it is done downloading by the event it fires. If the amount downloaded is the same as the total to download, then that was the last download that just happened. Also, check the success value of the event arguments. It will tell you if there was a problem downloading the link or not.


    Tell me what you think.
    Attached Files Attached Files

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    Hello Hellwraith,

    I really appreciate your help. But I still consider myself a newbie in VB.Net. I'm not asking you to do my assignment for me but I'm asking you to please help me in every step of the way.

    This is my first attempt on threading and I must admit that it's not that clear to me.

    Kindly take a look on my code.

    VB Code:
    1. Imports System.IO
    2. Imports System.IO.Directory
    3. Imports System.Data.SqlClient
    4. Imports System.Object
    5. Imports System.Threading
    6. Imports FileDownloader
    7.  
    8. Public Class downfile
    9.     Inherits System.Web.UI.Page
    10.     Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    11.     Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    12.     Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    13.     Protected WithEvents Button3 As System.Web.UI.WebControls.Button
    14.     Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    15.     Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox
    16.  
    17.     Private fdObject As New FileDownload()
    18.     Private theDownloadThread As Thread
    19.  
    20. #Region " Web Form Designer Generated Code "
    21.  
    22.     'This call is required by the Web Form Designer.
    23.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    24.  
    25.     End Sub
    26.  
    27.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    28.         'CODEGEN: This method call is required by the Web Form Designer
    29.         'Do not modify it using the code editor.
    30.         InitializeComponent()
    31.     End Sub
    32.  
    33. #End Region
    34.  
    35.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    36.         If Not Page.IsPostBack Then
    37.             BindData()
    38.         End If
    39.     End Sub
    40.  
    41.     Sub BindData()
    42.         Dim path As Path
    43.         Dim filepath As String = Session("userdir")
    44.         ListBox1.BorderStyle = BorderStyle.Solid
    45.         ListBox1.BorderColor = Color.Chocolate
    46.         Dim fileEntries As String() = Directory.GetFiles(filepath)
    47.         Dim fileName As String = path.GetFileName(filepath)
    48.         For Each fileName In fileEntries
    49.             ListBox1.Items.Add(path.GetFileName(fileName))
    50.         Next fileName
    51.     End Sub
    52.  
    53. ' this is my original download function
    54.     Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
    55.  
    56.         Dim path As Path
    57.  
    58.         Dim fullpath = path.GetFullPath(fname)
    59.         Dim name = path.GetFileName(fullpath)
    60.         Dim ext = path.GetExtension(fullpath)
    61.         Dim type As String = ""
    62.  
    63.         If Not IsDBNull(ext) Then
    64.             ext = LCase(ext)
    65.         End If
    66.  
    67.         Select Case ext
    68.             Case ".htm", ".html"
    69.                 type = "text/HTML"
    70.             Case ".txt"
    71.                 type = "text/plain"
    72.             Case ".doc", ".rtf"
    73.                 type = "Application/msword"
    74.             Case ".csv", ".xls"
    75.                 type = "Application/x-msexcel"
    76.             Case Else
    77.                 type = "text/plain"
    78.         End Select
    79.  
    80.         If (forceDownload) Then
    81.             Response.AppendHeader("content-disposition", _
    82.              "attachment; filename=" + name)
    83.         End If
    84.         If type <> "" Then
    85.             Response.ContentType = type
    86.         End If
    87.  
    88.         Response.WriteFile(fullpath)
    89.         Response.End()
    90.  
    91.     End Sub
    92.  
    93.  
    94.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    95.         Dim FilesToDownload(20) As String
    96.         Dim ictr As Integer = 0
    97.         Dim DownLoadItem As ListItem
    98.        
    99.         For Each DownLoadItem In ListBox1.Items
    100.             If DownLoadItem.Selected Then
    101.                 FilesToDownload(ictr) = DownLoadItem.Text
    102.                 ' Response.Write(FilesToDownload(ictr))
    103.             End If
    104.             ictr = ictr + 1
    105.         Next
    106.  
    107.         'Pass in the array to the FileDownload object.
    108.         fdObject = New FileDownload(FilesToDownload(ictr))
    109.  
    110. ' I dont know what's next; how to call the startdownload

    Thank you for any help.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    new code

    Hi Hellswraith,

    Pls disregard my previous post:

    This is my modified function:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         ' Dim FilesToDownload() As String
    3.         Dim FilesToDownload As New ArrayList()
    4.         Dim DownLoadItem As ListItem
    5.         Dim i As Integer
    6.         For i = 0 To ListBox1.Items.Count - 1
    7.             If ListBox1.Items(i).Selected Then
    8.                 'FilesToDownload(i) = ListBox1.Items(i).Text
    9.                 FilesToDownload.Add(ListBox1.Items(i).Text)
    10.             End If
    11.         Next
    12.  
    13.         'check items of array if correct
    14.         Dim j As Integer
    15.         For j = 0 To FilesToDownload.Count - 1
    16.             Response.Write(CStr(FilesToDownload.Item(j)))
    17.         Next
    18.  
    19.         'Pass in the array to the FileDownload object.
    20.         fdObject = New FileDownload(FilesToDownload(FilesToDownload.Count - 1))
    21.  
    22. ' how to pass array and path?? is this correct?

    I've used arraylist because the exact array size cannot be determine. It's based on the user selection.

    Many thanks.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    new code

    Hi Hellswraith,

    Pls disregard my previous post:

    This is my modified function:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         ' Dim FilesToDownload() As String
    3.         Dim FilesToDownload As New ArrayList()
    4.         Dim DownLoadItem As ListItem
    5.         Dim i As Integer
    6.         For i = 0 To ListBox1.Items.Count - 1
    7.             If ListBox1.Items(i).Selected Then
    8.                 'FilesToDownload(i) = ListBox1.Items(i).Text
    9.                 FilesToDownload.Add(ListBox1.Items(i).Text)
    10.             End If
    11.         Next
    12.  
    13.         'check items of array if correct
    14.         Dim j As Integer
    15.         For j = 0 To FilesToDownload.Count - 1
    16.             Response.Write(CStr(FilesToDownload.Item(j)))
    17.         Next
    18.  
    19.         'Pass in the array to the FileDownload object.
    20.         fdObject = New FileDownload(FilesToDownload(FilesToDownload.Count - 1))
    21.  
    22. ' how to pass array and path?? is this correct?

    I've used arraylist because the exact array size cannot be determine. It's based on the user selection.

    Many thanks.

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Well, I built this project for you in VB that shows you how to use my download class....but then I realized that you are trying to do this from the server end, is that true? If so, I have no idea how to do what you are asking.

    Here is the project I built for you anyway.
    Attached Files Attached Files

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    Hi,

    Yup, the files are coming from the server. I was about to give up and conclude that downloading multiple files is not possible based on my research. But you had me on the idea of threading so I hang on a little bit.

    Well, now coming from an expert, I'm now confident to tell my boss that it's really not possible.

    Thank you very much for your help and effort.

    I really appreciate them.

Posting Permissions

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



Click Here to Expand Forum to Full Width