|
-
Feb 20th, 2003, 08:38 PM
#1
Thread Starter
Addicted Member
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.
-
Feb 20th, 2003, 09:04 PM
#2
Sleep mode
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 ?
-
Feb 21st, 2003, 05:48 AM
#3
Lively Member
Are you doing something like:
Code:
For Each item in myListBox.SelectedItems
Download(item.myURL);
Next
?
-
Feb 21st, 2003, 08:56 PM
#4
Thread Starter
Addicted Member
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:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer = 0
Dim item As ListItem
For i = 0 To ListBox1.Items.Count - 1
If item.Selected Then
listitem = ListBox1.Items(i).Text
DownloadFile(Session("userdir") & listitem, True)
End If
Next
End Sub
Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
Dim path As Path
Dim fullpath = path.GetFullPath(fname)
Dim name = path.GetFileName(fullpath)
Dim ext = path.GetExtension(fullpath)
Dim type As String = ""
If Not IsDBNull(ext) Then
ext = LCase(ext)
End If
Select Case ext
Case ".htm", ".html"
type = "text/HTML"
Case ".txt"
type = "text/plain"
Case ".doc", ".rtf"
type = "Application/msword"
Case ".csv", ".xls"
type = "Application/x-msexcel"
Case Else
type = "text/plain"
End Select
If (forceDownload) Then
Response.AppendHeader("content-disposition", _
"attachment; filename=" + name)
End If
If type <> "" Then
Response.ContentType = type
End If
Response.WriteFile(fullpath)
Response.End()
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.
-
Feb 21st, 2003, 09:13 PM
#5
Sleep mode
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:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer = 0
Dim item As ListItem
For i = 0 To ListBox1.Items.Count - 1
If item.Selected Then
listitem = ListBox1.Items(i).Text
DownloadFile(Session("userdir") & listitem, True)
End If
Next
End Sub
Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
Dim path As Path
Dim fullpath = path.GetFullPath(fname)
Dim name = path.GetFileName(fullpath)
Dim ext = path.GetExtension(fullpath)
Dim type As String = ""
If Not IsDBNull(ext) Then
ext = LCase(ext)
End If
Select Case ext
Case ".htm", ".html"
type = "text/HTML"
Case ".txt"
type = "text/plain"
Case ".doc", ".rtf"
type = "Application/msword"
Case ".csv", ".xls"
type = "Application/x-msexcel"
Case Else
type = "text/plain"
End Select
If (forceDownload) Then
Response.AppendHeader("content-disposition", _
"attachment; filename=" + name)
End If
If type <> "" Then
Response.ContentType = type
End If
Response.WriteFile(fullpath)
Response.End()
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 ??
-
Feb 21st, 2003, 09:47 PM
#6
Thread Starter
Addicted Member
Sorry I've messed up with my code: It should be like this:
VB Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim item As ListItem
For Each item In ListBox1.Items
If item.Selected Then
DownloadFile(Session("userdir") & item.Text, True)
End If
Next
End Sub
Pirate: I think its the listitem class which represents a data item in a data-bound list control.
-
Feb 21st, 2003, 09:53 PM
#7
Sleep mode
Originally posted by Marivic
Sorry I've messed up with my code: It should be like this:
VB Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim item As ListItem
For Each item In ListBox1.Items
If item.Selected Then
DownloadFile(Session("userdir") & item.Text, True)
End If
Next
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 ?
-
Feb 21st, 2003, 10:18 PM
#8
Thread Starter
Addicted Member
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.
-
Feb 21st, 2003, 10:49 PM
#9
Sleep mode
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 .
-
Feb 21st, 2003, 10:52 PM
#10
Sleep mode
try this now . It shows all selected items but with noisy error
VB Code:
Dim si As New ListBox.SelectedIndexCollection(ListBox1)
Dim lop As Integer
Try
For lop = 0 To ListBox1.Items.Count - 1
MsgBox(si.Item(lop.ToString))
Next
Catch x As Exception
MsgBox(x.Message)
End Try
-
Feb 21st, 2003, 11:23 PM
#11
Sleep mode
Did you get the error msg ???
-
Feb 22nd, 2003, 04:39 AM
#12
Thread Starter
Addicted Member
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.
-
Feb 22nd, 2003, 12:25 PM
#13
PowerPoster
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.
-
Feb 22nd, 2003, 12:48 PM
#14
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|