Short VB.NET Project for Filedialog and FTP upload

K

Kai Apel

Dear Newsgroup,

My name is Kai from Germany and I`m trying to learn VB.Net. My first
Project is a simple Form with a button, witch is starting the
filedialog for multiselect file. All thats fine. Now I want
automaticly upload this selected file via FTP.

I'm using Windows.Forms:

My Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDialog.Click

Dim myStream As Stream = Nothing
Dim ofd As New OpenFileDialog()

With ofd
.InitialDirectory = "C:\" 'Preselection Filelocation
.Title = "Selected Files" 'Title
.Filter = "MP3-Format (*.mp3)|*.mp3|WAVE-Format (*.wav)|
*.wav" 'Filefilter
.FilterIndex = 2 'Preselectet Extension
.RestoreDirectory = False
.Multiselect = True 'to select multiple files
End With

If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK
Then
Try
myStream = ofd.OpenFile()
If (myStream IsNot Nothing) Then


'Here I need to place the Code for Uploading the selected files to a
webserver via ftp


Else
MsgBox("Nothing selected!", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly)
Exit Sub
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original
error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we
didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
 
K

kimiraikkonen

Dear Newsgroup,

My name is Kai from Germany and I`m trying to learn VB.Net. My first
Project is a simple Form with a button, witch is starting the
filedialog for multiselect file. All thats fine. Now I want
automaticly upload this selected file via FTP.

I'm using Windows.Forms:

My Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDialog.Click

        Dim myStream As Stream = Nothing
        Dim ofd As New OpenFileDialog()

With ofd
            .InitialDirectory = "C:\" 'Preselection Filelocation
            .Title = "Selected Files" 'Title
            .Filter = "MP3-Format (*.mp3)|*.mp3|WAVE-Format(*.wav)|
*.wav" 'Filefilter
            .FilterIndex = 2 'Preselectet Extension
            .RestoreDirectory = False
            .Multiselect = True 'to select multiple files
End With

        If ofd.ShowDialog() = System.Windows.Forms.DialogResult..OK
Then
            Try
                myStream = ofd.OpenFile()
                If (myStream IsNot Nothing) Then

'Here I need to place the Code for Uploading the selected files to a
webserver via ftp

                Else
                    MsgBox("Nothing selected!", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly)
                    Exit Sub
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original
error: " & Ex.Message)
            Finally
                ' Check this again, since we need to makesure we
didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If

Hi,
To try to upload multiple selected files to your FTP server you can
try this:

First retrieve multi-selected filenames in a for-each loop then upload
each file using My.Computer.Network.UploadFile method:

'--------------------------------------
'.......The rest is above in your code
For Each file As String In ofd.FileNames

'Return filenames with extension
Dim filename As String
filename = System.IO.Path.GetFileName(file)

' Do the uploading
My.Computer.Network.UploadFile(file, _
"ftp://domain.com/folder/" & filename, _
"user", "pass")

Next
'--------------------------------------

A few notes, uploading multiple files at once may (and probably will)
block your UI (form) and you may need to go with multi-threaded
approach like using BackgroundWorker. Also, you can think other usage
of UploadFile method, so see the overload list here:

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.devices.network.uploadfile(VS.80).aspx


Hope this helps,

Onur Güzel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top