|
|
Title | Let the user drag files into a list and then upload them all to a Web site in Visual Basic .NET |
Description | This example shows how to let the user drag files into a list and then upload them all to a Web site in Visual Basic .NET. |
Keywords | upload, FTP, file transfer, FtpWebRequest, WebRequest, Visual Basic .NET, VB.NET |
Categories | Internet, Files and Directories |
|
|
The program lets the user drag files into its ListBox. To allow that, the program must set the ListBox's AllowDrop property to True.
When the user drags something over the ListBox, its DragEnter event handler executes. It sets the e.Effect parameter to Copy if the user is dragging files.
|
|
Private Sub lstFiles_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles _
lstFiles.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
|
|
When the user drops the files, the program loops through the file names and adds them to the list.
|
|
Private Sub lstFiles_DragDrop(ByVal sender As Object, ByVal _
e As System.Windows.Forms.DragEventArgs) Handles _
lstFiles.DragDrop
For Each filename As String In _
e.Data.GetData(DataFormats.FileDrop, True)
If Not lstFiles.Items.Contains(filename) Then
lstFiles.Items.Add(filename)
End If
Next filename
End Sub
|
|
When the user clicks the Go button, the following code executes. It uses the user-entered host name and remote directory to build the remote host's name in the form:
ftp://www.myhost.com/remote_directory/
The code saves the entered user name and password, and then loops through the files in the list. For each file, it calls subroutine UploadFile and removes the file from the list.
|
|
Private Sub btnGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGo.Click
Static running As Boolean = False
running = Not running
If running Then
Me.Cursor = Cursors.WaitCursor
' Build the remote file path as in:
' ftp://www.myhost.com/public_html/
Dim remote_path As String = txtHost.Text
If Not remote_path.ToLower().StartsWith("ftp://") _
Then
remote_path = "ftp://" & remote_path
End If
If Not remote_path.EndsWith("/") Then remote_path _
&= "/"
remote_path &= txtDirectory.Text
If Not remote_path.EndsWith("/") Then remote_path _
&= "/"
Dim user_name As String = txtUserName.Text
Dim password As String = txtPassword.Text
Dim num_copied As Integer = 0
Do While lstFiles.Items.Count > 0
Try
UploadFile(lstFiles.Items(0), remote_path, _
user_name, password)
lstFiles.Items.RemoveAt(0)
num_copied += 1
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Exit Do
End Try
Application.DoEvents()
If Not running Then Exit Do
Loop
running = False
Me.Cursor = Cursors.Default
MessageBox.Show("Copied " & num_copied & " files", _
"Done", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
End Sub
|
|
Subroutine UploadFile composes the remote file's name and makes a FtpWebRequest object to represent uploading to that file. The code makes credentials for the entered user name and password.
Next the code copies the input file into a byte array. It opens the request stream for the upload and writes the bytes into it. Finally the code closes the stream and checks the result.
|
|
' Upload the file.
Private Sub UploadFile(ByVal local_file As String, ByVal _
remote_path As String, ByVal user_name As String, ByVal _
password As String)
' Get the object used to communicate with the server.
Dim fi As New FileInfo(local_file)
Dim remote_file As String = remote_path & fi.Name
' Make the WebRequest.
Dim request As FtpWebRequest = _
WebRequest.Create(remote_file)
request.Method = WebRequestMethods.Ftp.UploadFile
' Get credentials.
request.Credentials = New NetworkCredential(user_name, _
password)
' Copy the file into a Byte array.
Dim source_stream As New StreamReader(local_file)
Dim file_bytes As Byte() = _
Encoding.UTF8.GetBytes(source_stream.ReadToEnd())
source_stream.Close()
request.ContentLength = file_bytes.Length
' Open the request stream and write the bytes into it.
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(file_bytes, 0, file_bytes.Length)
requestStream.Close()
' Check the response.
Dim response As FtpWebResponse = request.GetResponse()
If Not response.StatusDescription.Contains("File " & _
"successfully transferred") Then
Dim msg As String = "Error uploading file " & _
local_file & vbCrLf & response.StatusDescription
response.Close()
Throw New Exception(msg)
End If
response.Close()
End Sub
|
|
|
|
|
|