Results 1 to 14 of 14

Thread: Downloading multiple files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    Downloading multiple files

    Hi All,

    I've created a simple download utility wherein directory contents are displayed in a listbox and downloaded one by one. I tried to select multiple files from the listbox to download, but still, it only downloaded a single file. My question is, is it possible to download multiple files in one click.

    Thanks.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Downloading multiple files

    Originally posted by Marivic
    Hi All,

    I've created a simple download utility wherein directory contents are displayed in a listbox and downloaded one by one. I tried to select multiple files from the listbox to download, but still, it only downloaded a single file. My question is, is it possible to download multiple files in one click.

    Thanks.

    I'm not sure what do you mean but you can download multiple files at the same time . Just multiply the connection to the server you are downloading files from .Was that your question ?

  3. #3
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    Are you doing something like:

    Code:
    For Each item in myListBox.SelectedItems
        Download(item.myURL);
    Next
    ?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    Pirate: Thanks for replying but I'm afraid, I don't understand your question. Please bear with a newbie.

    hbandarra:
    Yes, something like that: Here is my code:

    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim i As Integer = 0
    3.         Dim item As ListItem
    4.           For i = 0 To ListBox1.Items.Count - 1
    5.                   If item.Selected Then
    6.                         listitem = ListBox1.Items(i).Text
    7.                         DownloadFile(Session("userdir") & listitem, True)
    8.                  End If
    9.              Next
    10.    End Sub
    11.  
    12. Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
    13.  
    14.         Dim path As Path
    15.  
    16.         Dim fullpath = path.GetFullPath(fname)
    17.         Dim name = path.GetFileName(fullpath)
    18.         Dim ext = path.GetExtension(fullpath)
    19.         Dim type As String = ""
    20.  
    21.         If Not IsDBNull(ext) Then
    22.             ext = LCase(ext)
    23.         End If
    24.  
    25.         Select Case ext
    26.             Case ".htm", ".html"
    27.                 type = "text/HTML"
    28.             Case ".txt"
    29.                 type = "text/plain"
    30.             Case ".doc", ".rtf"
    31.                 type = "Application/msword"
    32.             Case ".csv", ".xls"
    33.                 type = "Application/x-msexcel"
    34.             Case Else
    35.                 type = "text/plain"
    36.         End Select
    37.  
    38.         If (forceDownload) Then
    39.             Response.AppendHeader("content-disposition", _
    40.            "attachment; filename=" + name)
    41.         End If
    42.         If type <> "" Then
    43.             Response.ContentType = type
    44.         End If
    45.  
    46.         Response.WriteFile(fullpath)
    47.         Response.End()
    48.  
    49.     End Sub

    Upon pressing the button to download all that are selected from the listbox, it will display a dialog box to save or open the first selected file, then that's it. It never asks for the next selected items.

    Thanks for any help.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Marivic
    Pirate: Thanks for replying but I'm afraid, I don't understand your question. Please bear with a newbie.

    hbandarra:
    Yes, something like that: Here is my code:

    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim i As Integer = 0
    3.         Dim item As ListItem
    4.           For i = 0 To ListBox1.Items.Count - 1
    5.                   If item.Selected Then
    6.                         listitem = ListBox1.Items(i).Text
    7.                         DownloadFile(Session("userdir") & listitem, True)
    8.                  End If
    9.              Next
    10.    End Sub
    11.  
    12. Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
    13.  
    14.         Dim path As Path
    15.  
    16.         Dim fullpath = path.GetFullPath(fname)
    17.         Dim name = path.GetFileName(fullpath)
    18.         Dim ext = path.GetExtension(fullpath)
    19.         Dim type As String = ""
    20.  
    21.         If Not IsDBNull(ext) Then
    22.             ext = LCase(ext)
    23.         End If
    24.  
    25.         Select Case ext
    26.             Case ".htm", ".html"
    27.                 type = "text/HTML"
    28.             Case ".txt"
    29.                 type = "text/plain"
    30.             Case ".doc", ".rtf"
    31.                 type = "Application/msword"
    32.             Case ".csv", ".xls"
    33.                 type = "Application/x-msexcel"
    34.             Case Else
    35.                 type = "text/plain"
    36.         End Select
    37.  
    38.         If (forceDownload) Then
    39.             Response.AppendHeader("content-disposition", _
    40.            "attachment; filename=" + name)
    41.         End If
    42.         If type <> "" Then
    43.             Response.ContentType = type
    44.         End If
    45.  
    46.         Response.WriteFile(fullpath)
    47.         Response.End()
    48.  
    49.     End Sub

    Upon pressing the button to download all that are selected from the listbox, it will display a dialog box to save or open the first selected file, then that's it. It never asks for the next selected items.

    Thanks for any help.
    I can't find this object
    ListItem
    Where did you get it from ??

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    Sorry I've messed up with my code: It should be like this:

    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim item As ListItem
    3.         For Each item In ListBox1.Items
    4.                If item.Selected Then
    5.                     DownloadFile(Session("userdir") & item.Text, True)
    6.                 End If
    7.         Next
    8.     End Sub


    Pirate: I think its the listitem class which represents a data item in a data-bound list control.

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Marivic
    Sorry I've messed up with my code: It should be like this:

    VB Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Dim item As ListItem
    3.         For Each item In ListBox1.Items
    4.                If item.Selected Then
    5.                     DownloadFile(Session("userdir") & item.Text, True)
    6.                 End If
    7.         Next
    8.     End Sub
    Pirate: I think its the listitem class which represents a data item in a data-bound list control.
    I couldn't use this :
    Dim item As ListItem
    it isn't defined . How did you do it ?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    I wish I could answer you but I don't know why. It's just exist in the (is it?) intellisense? When you type 'dim item as .....you will be presented with options and LISTITEM is there!

    Please note that this a web apps not a windows apps.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Marivic
    I wish I could answer you but I don't know why. It's just exist in the (is it?) intellisense? When you type 'dim item as .....you will be presented with options and LISTITEM is there!
    Please note that this a web apps not a windows apps.
    Intellisense is not showing "ListItem" . It isn't even exist in Listbox object .

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    try this now . It shows all selected items but with noisy error
    VB Code:
    1. Dim si As New ListBox.SelectedIndexCollection(ListBox1)
    2.         Dim lop As Integer
    3.         Try
    4.             For lop = 0 To ListBox1.Items.Count - 1
    5.                 MsgBox(si.Item(lop.ToString))
    6.             Next
    7.         Catch x As Exception
    8.             MsgBox(x.Message)
    9.         End Try

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Did you get the error msg ???

  12. #12

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

    Thanks for helping but my problem is not with the listbox items. It is in the downloading of files. When I select multiple files from the listbox, a dialog box will be displayed asking to save or open the first selected file. After saving the file to the local file system, it wont ask for the second selected file and so on.

    If you will notice my for loop:

    [Highlight=VB]
    For Each item In ListBox1.Items
    If item.Selected Then
    DownloadFile(Session("userdir") & item.Text, True)
    End If
    Next
    [/vbcode

    the Downloadfile sub should be fired in every selected item, but it seems that it worked only once.

    Pls help.

  13. #13
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Thanks for helping but my problem is not with the listbox items. It is in the downloading of files. When I select multiple files from the listbox, a dialog box will be displayed asking to save or open the first selected file. After saving the file to the local file system, it wont ask for the second selected file and so on.
    The reason why is because it is going through the loop really fast and not waiting until the first file is downloaded before doing it again...my non technical explaination

    What you are going to have to do is create a download class. In this class, you are going to want to pass in a array of files. Then have a method in this class that starts downloading. You would call that.

    Your class would then download the first item, then raise an event in the same class to tell the class to download the next item. It is best if you start the download process on another thread so it doesn't freeze your whole application.

    I have done this in C# and will try to find them and post them here. If you don't mind converting them they should work for you.

  14. #14
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Like I said, I have the code that will do what you want, but it is in C#. You can just throw the FileDownload class in a C# project, compile the dll, and reference it from VB. The form code though you will have to convert to VB.

    Put this code in its own class file. It is the download object that will manage the downloads.
    Code:
    using System;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;
    
    namespace Downloader
    {
    	/// <summary>
    	/// Downloads files from the Internet.
    	/// Author: Brian Russell
    	/// Email: [email protected]
    	/// Comments:  If you find you need to use this code, it is fine with me.  Just
    	/// make sure that I am credited somehow in the comments.
    	/// </summary>
    	public class FileDownload
    	{
    		#region Class Variables
    		string[] sLinks;
    		bool bCancel;
    		#endregion
    
    		#region Class Constructors
    		public FileDownload()
    		{
    			// Set the cancel variable to false.
    			bCancel = false;
    		}
    
    		public FileDownload(params string [] links)
    		{
    			// Fill the links array.
    			sLinks = new string[links.Length];
    			links.CopyTo(sLinks,0);
    
    			// Set the cancel variable to false.
    			bCancel = false;
    		}
    		#endregion
    
    		#region Class Methods
    		public void StartDownload()
    		{
    			// Loop through each link to download.
    			for(int i = 0; i < sLinks.Length; i++)
    			{
    				string[] sFileParts;
    				string sFile = sLinks[i];
    				string sFileName;
    				int iStringLength = sFile.Length;
    
    				// Find a file name to save as.  This does this by getting the
    				// directory the user selected to save the files to, then adding
    				// the date in a yymmddhhmmss format along with a dash and then
    				// the original file name.
    				sFileParts = sFile.Split(new Char[] {'/'});
    				sFileName = @"C:\"; // You put the directory here.
    				sFileName += DateTime.Now.ToString("yymmddhhmmss");
    				sFileName += "-";
    				sFileName += sFileParts[sFileParts.GetUpperBound(0)].ToString();
    				
    				WebClient client = new WebClient();
    
    				try
    				{
    					// Download the file to the hard drive.
    					client.DownloadFile(sLinks[i].ToString(), sFileName);
    
    					// Call an event showing the link was downloaded
    					FileDownloadCompleted(sLinks[i].ToString(), sLinks.Length, (i + 1));
    				}
    				catch
    				{
    					// Error in the download.
    					// Call an event showing the link was downloaded
    					FileDownloadCompleted(sLinks[i].ToString() + " - Error", sLinks.Length, (i + 1));
    
    					// Need to erase the bogus file if created.
    					FileInfo theFile = new FileInfo(sFileName);
    					if(theFile.Exists)
    					{
    						// File was created, need to delete it.
    						theFile.Delete();
    					}
    				}
    				
    				// Let other pending events go through.  That way we can see a cancel
    				// request.
    				Application.DoEvents();
    
    				// Check to see if the download was aborted
    				if(bCancel == true)
    					return;
    			}
    		}
    
    		public void CancelDownload()
    		{
    			// This method is called by the user to abort the download process.
    			// Change the cancel variable to true so the download method will
    			// see that the user wants to abort.
    			bCancel = true;
    		}
    		#endregion
    
    		#region Class Properties
    		public string[] Links
    		{		
    			// Files the link array that will be downloaded.
    			// Write only.
    			set
    			{
    				sLinks = new string[value.Length];
    				value.CopyTo(sLinks,0);
    			}
    		}
    		#endregion
    
    		#region Class Event/Delegate Code
    
    		public delegate void DownloadCompleteHandler(object FileDownload, DownloadArgs DLInfo);
    
    		public event DownloadCompleteHandler OnFileDownloadComplete;
    
    		private void FileDownloadCompleted(string sFileDownloaded, int iLinksToDownload, int iLinksDownloaded)
    		{
    			DownloadArgs dlInfo = new DownloadArgs(sFileDownloaded, iLinksToDownload, iLinksDownloaded);
    			if(OnFileDownloadComplete != null)
    			{
    				OnFileDownloadComplete(this,dlInfo);
    			}
    		}
    		#endregion
    	}
    
    	public class DownloadArgs : EventArgs
    	{
    		#region Constructor
    		public DownloadArgs(string sFileDownloaded, int iLinksToDownload, int iLinksDownloaded)
    		{
    			this.LinkAmountDownloaded = iLinksDownloaded;
    			this.LinkAmountToDownload = iLinksToDownload;
    			this.LinkDownloaded = sFileDownloaded;
    		}
    		#endregion
    
    		#region Class Variables
    		public readonly int LinkAmountToDownload;
    		public readonly int LinkAmountDownloaded;
    		public readonly string LinkDownloaded;
    		#endregion
    	}
    }
    Use this code in the form code.
    Code:
    // Downloads files from the Internet.
    // Author: Brian Russell
    // Email: [email protected]
    // Comments:  If you find you need to use this code, it is fine with me.  Just
    // make sure that I am credited somehow in the comments.
    
    
    // holds instances of the objects needed globally.
    private FileDownload fdObject;
    private Thread theDownloadThread;
    
    private void DownloadFiles()
    {
    	string[] sLinksToDownload;
    
    	// Read in all the links into a string array.  You will have to change this
    	// to read in your links.
    	sLinksToDownload = new string[lbFilesToBeDownloaded.Items.Count];
    	for(int iCount = 0; iCount < lbFilesToBeDownloaded.Items.Count; iCount++)
    	{
    		sLinksToDownload[iCount] = lbFilesToBeDownloaded.Items[iCount].ToString();
    	}
    
    	// Pass in the array to the FileDownload object.
    	fdObject = new FileDownload(sLinksToDownload);
    
    	// Add the event handler for the FileDownload object so we know when files are downloaded.
    	fdObject.OnFileDownloadComplete += new FileDownload.DownloadCompleteHandler(this.FileDownloaded);
    
    	// Set this up on it's own thread.  This way the user can continue doing things.
    	Thread fileDLThread = new Thread(new ThreadStart(this.StartDLOnThread));
    			
    	// Send the reference of the new thread to the global variable so we can manage it later.
    	theDownloadThread = fileDLThread;
    
    	fileDLThread.Start();
    
    	// This method is done, the event will fire when a download is complete.
    	// That event will figure out when it is done downloading files.
    }
    		
    private void StartDLOnThread()
    {
    	// Call the start download method.
    	fdObject.StartDownload();
    }
    
    private void FileDownloaded(object sender, DownloadArgs DLInfo)
    {
    	// How many links to download total.
    	string linksToDownload = DLInfo.LinkAmountToDownload.ToString();
    	// How many links have been downloaded.
    	string linksDownloaded = DLInfo.LinkAmountDownloaded.ToString();
    	// Figure out how many links still need downloaded.
    	int iToGo;
    	iToGo = DLInfo.LinkAmountToDownload - DLInfo.LinkAmountDownloaded;
    
    
    	// Shows how to get the last item just downloaded.  You can put whatever here
    	// just helps you show which item was downloaded last.
    	lbFilesDownloaded.Items.Add(DLInfo.LinkDownloaded.ToString());
    
    	// Now check to see if it is done downloading files.
    	if(iToGo == 0)
    	{
    		// This means that the downloader has downloaded the last file.
    	
    	}
    }
    		
    private void CancelDownload()
    {
    	// Gets the download thread, and aborts the operation.
    	Thread temp = theDownloadThread;
    	temp.Abort();
    }

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