Short VB.NET Project for Filedialog and FTP upload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kai Apel

    Short VB.NET Project for Filedialog and FTP upload

    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles cmdDialog.Click

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

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

    If ofd.ShowDialog( ) = System.Windows. Forms.DialogRes ult.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.Cri tical +
    MsgBoxStyle.OkO nly)
    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
  • Armin Zingler

    #2
    Re: Short VB.NET Project for Filedialog and FTP upload

    "Kai Apel" <mail@kaiapel.d eschrieb
    Dear Newsgroup,
    >
    My name is Kai from Germany
    There is also
    microsoft.publi c.de.german.ent wickler.dotnet. vb
    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.
    Learn about the comprehensive support for the FTP protocol that .NET Framework provides with the FtpWebRequest and FtpWebResponse classes.

    (sub topic "upload..." )



    Armin

    Comment

    • kimiraikkonen

      #3
      Re: Short VB.NET Project for Filedialog and FTP upload

      On Oct 19, 6:51 pm, Kai Apel <m...@kaiapel.d ewrote:
      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(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles cmdDialog.Click
      >
              Dim myStream As Stream = Nothing
              Dim ofd As New OpenFileDialog( )
      >
      With ofd
                  .InitialDirecto ry = "C:\" 'Preselection Filelocation
                  .Title = "Selected Files" 'Title
                  .Filter = "MP3-Format (*.mp3)|*.mp3|W AVE-Format(*.wav)|
      *.wav" 'Filefilter
                  .FilterIndex = 2 'Preselectet Extension
                  .RestoreDirecto ry = False
                  .Multiselect = True 'to select multiple files
      End With
      >
              If ofd.ShowDialog( ) = System.Windows. Forms.DialogRes ult..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.Cri tical +
      MsgBoxStyle.OkO nly)
                          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.Net work.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(fil e)

      ' Do the uploading
      My.Computer.Net work.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 BackgroundWorke r. Also, you can think other usage
      of UploadFile method, so see the overload list here:




      Hope this helps,

      Onur Güzel

      Comment

      • Kai Apel

        #4
        Re: Short VB.NET Project for Filedialog and FTP upload

        thx for the answer.

        K

        Comment

        Working...