Upload Multiple files to a ftp server

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

59 Replies - 11067 Views - Last Post: 23 January 2013 - 11:04 AM Rate Topic: ***** 1 Votes

#1 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Upload Multiple files to a ftp server

Posted 26 August 2012 - 10:35 PM

Hey i use the code below to upload a single file to a server and get the progress of the upload is there anyway of implementing a multiple upload system where the user can select multiple files which are added to a listview and then the code checks if the files have been uploaded and if not it uploads them one by one till the end.
Private WithEvents myFtpUploadWebClient As New WebClient

 'here you track what happened when upload completes
    Private Sub myFtpUploadWebClient_UploadFileCompleted(ByVal sender As Object, ByVal e As System.Net.UploadFileCompletedEventArgs) Handles myFtpUploadWebClient.UploadFileCompleted

        'If we didn't succeed, we want to know what went wrong
        If e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message)

        Else
            MessageBox.Show("File upload complete!")
        End If
    End Sub

    'and here we listen to WebClient's event when UL progress changes
    Private Sub myFtpUploadWebClient_UploadProgressChanged(ByVal sender As Object, ByVal e As System.Net.UploadProgressChangedEventArgs) Handles myFtpUploadWebClient.UploadProgressChanged
        MbProgressBar1.Value = e.ProgressPercentage
    End Sub


    Private Sub SelectFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFileButton.Click
        'ofd is OpenFileDialog
        OpenFileDialog1.ShowDialog()
        OpenFileDialog1.Title = "Chose a File"

        'Before you connect, and try to upload something to FTP, you must set everything up:
        'txtUploadTo, txtLoginUserName and txtLoginPassword are TextBox components
        'Uri has to have first argument as full path with file name and file extension (like .html or something). 2nd parameter is pure file name, without the path
        Dim myUri As New Uri("ftp://testing.vacau.com/public_html/" & OpenFileDialog1.SafeFileName)
        myFtpUploadWebClient.Credentials = New System.Net.NetworkCredential("Usrname", "Pass")

        'Now you do the uploading
        myFtpUploadWebClient.UploadFileAsync(myUri, OpenFileDialog1.FileName)

    End Sub


Here as you can see when you click the button a single file automatically starts uploading.
But is there a way of letting the user select multiple files which are then added to a listview and then for the code to keep checking and upload all the files?

Is This A Good Question/Topic? 0
  • +

Replies To: Upload Multiple files to a ftp server

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 26 August 2012 - 10:54 PM

Of course you can. OpenFileDialog has multiselect property. Look at the example code and adjust it to your needs. You'll probably want to separate file selection, and start uploading process. Than you'll probably want to show progress change for each of the uploading file, but we can talk about that later, when you'll have working solution for uploading more than one file. It's not that hard, just try to do it.
Was This Post Helpful? 1
  • +
  • -

#3 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 28 August 2012 - 09:45 AM

View Postlucky3, on 26 August 2012 - 10:54 PM, said:

Of course you can. OpenFileDialog has multiselect property. Look at the example code and adjust it to your needs. You'll probably want to separate file selection, and start uploading process. Than you'll probably want to show progress change for each of the uploading file, but we can talk about that later, when you'll have working solution for uploading more than one file. It's not that hard, just try to do it.

Ok when i select multiple files i get all the filenames as "String[]array" as different items in a listbox.
Im not very good at this type of coding could you tell me whats wrong here.
 Dim dr As DialogResult = Me.OpenFileDialog1.ShowDialog()
        If (dr = System.Windows.Forms.DialogResult.OK) Then
            ' Read the files 
            Dim file As String
            For Each file In OpenFileDialog1.FileNames

                Try

                    ListBox1.Items.Add(OpenFileDialog1.FileNames)

                Catch SecEx As SecurityException
                    ' The user lacks appropriate permissions to read files, discover paths, etc.
                    MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                        "Error message: " & SecEx.Message & "\n\n" & _
                        "Details (send to Support):\n\n" & SecEx.StackTrace)
                Catch ex As Exception
                    ' Could not load the File - probably permissions-related.
                    MessageBox.Show(("Cannot Select File: " & file.Substring(file.LastIndexOf("\"c)) & _
                    ". You may not have permission to read the file, or " + "it may be corrupt." _
                    & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
                End Try
            Next file
        End If


Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 28 August 2012 - 10:34 AM

Change from:
For Each file In OpenFileDialog1.FileNames

               Try

                   ListBox1.Items.Add(OpenFileDialog1.FileNames)





to:
For Each file In OpenFileDialog1.FileNames

               Try

                   ListBox1.Items.Add(file)



Was This Post Helpful? 1
  • +
  • -

#5 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 29 August 2012 - 12:59 AM

Should it look like this for selecting each file individually and then uploading them one by one ?
 Dim dr As DialogResult = Me.OpenFileDialog1.ShowDialog()
        If (dr = System.Windows.Forms.DialogResult.OK) Then
            ' Read the files 
            Dim file As String
            For Each file In OpenFileDialog1.FileNames

                Try

                    ListBox1.Items.Add(file)

                    Dim myUri As New Uri("ftp://testing.com/public_html/" & OpenFileDialog1.SafeFileName)
                    myFtpUploadWebClient.Credentials = New System.Net.NetworkCredential("", "")

                    'Now you do the uploading
                    myFtpUploadWebClient.UploadFileAsync(myUri, file)


                Catch SecEx As SecurityException
                    ' The user lacks appropriate permissions to read files, discover paths, etc.
                    MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                        "Error message: " & SecEx.Message & "\n\n" & _
                        "Details (send to Support):\n\n" & SecEx.StackTrace)
                Catch ex As Exception
                    ' Could not load the File - probably permissions-related.
                    MessageBox.Show(("Cannot Select File: " & file.Substring(file.LastIndexOf("\"c)) & _
                    ". You may not have permission to read the file, or " + "it may be corrupt." _
                    & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
                End Try
            Next file
        End If


Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 29 August 2012 - 05:57 AM

View PostBluezap, on 29 August 2012 - 12:59 AM, said:

Should it look like this for selecting each file individually and then uploading them one by one ?


You are close, but you need to learn something essential: how to debug your code. Than compare your current code with one file uploading code in a separate project, and see in what form are uri values there, and what they should be in multiple file upload code. Than you'll have it done in some basic form.
Was This Post Helpful? 1
  • +
  • -

#7 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,507
  • Joined: 16-March 11

Re: Upload Multiple files to a ftp server

Posted 29 August 2012 - 08:11 AM

I would suggest that you should learn to use the ftpwebrequest class for FTP tasks. It was truly designed for this.

FTPWebRequest Class
Was This Post Helpful? 0
  • +
  • -

#8 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 29 August 2012 - 10:25 AM

View Postlucky3, on 29 August 2012 - 05:57 AM, said:

You are close

What i don't understand is this -
Now when multiple files are selected how do i change this line so that the file names keeping changing for each upload? Isn't it impossible to use "&" and then use openfiledialog1.Safefilenames
Dim myUri As New Uri("ftp://testingbluezap.vacau.com/public_html/" & OpenFileDialog1.SafeFileName)


But the logic of uploading the files is correct right?
  myFtpUploadWebClient.UploadFileAsync(myUri, file)


This post has been edited by Bluezap: 29 August 2012 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#9 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 29 August 2012 - 12:11 PM

You are right in both cases. SafeFileNames is array of values of String type. Each array value has its own index. On SafeFileNames(0), you'd find String that matches the text behind last / in FileNames(0).

So for example if you select 3 files: file1.txt, file2.txt and file3.txt from C:\

in FileNames array, you'd have:
  • C:\file1.txt
  • C:\file2.txt
  • C:\file3.txt


and in SafeFileNames:
  • file1.txt
  • file2.txt
  • file3.txt


So FileNames(0) = "C:\file1.txt" and SafeFileNames(0) = "file1.txt".

For you to have the right String for the file name, you need to change SafeFileNames elemnets index each time the For Each loop runs through the lines of code it has. The easiest way for you could be to declare a new variable (outside the for each loop) that would represent a counter (think of the apropriate type for this variable). Than assign a starting value to it, that would be used for the value of SafeFileNames index. At the end of the loop, you need to increment this index variable, and everything will work as it needs to. Please, do this thing by yourself.
Was This Post Helpful? 1
  • +
  • -

#10 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 30 August 2012 - 12:51 AM

View Postlucky3, on 29 August 2012 - 12:11 PM, said:

You are right in both cases. SafeFileNames is array of values of String type. Each array value has its own index. On SafeFileNames(0), you'd find String that matches the text behind last / in FileNames(0).

So for example if you select 3 files: file1.txt, file2.txt and file3.txt from C:\

in FileNames array, you'd have:
  • C:\file1.txt
  • C:\file2.txt
  • C:\file3.txt


and in SafeFileNames:
  • file1.txt
  • file2.txt
  • file3.txt


So FileNames(0) = "C:\file1.txt" and SafeFileNames(0) = "file1.txt".

For you to have the right String for the file name, you need to change SafeFileNames elemnets index each time the For Each loop runs through the lines of code it has. The easiest way for you could be to declare a new variable (outside the for each loop) that would represent a counter (think of the apropriate type for this variable). Than assign a starting value to it, that would be used for the value of SafeFileNames index. At the end of the loop, you need to increment this index variable, and everything will work as it needs to. Please, do this thing by yourself.

Does this seem to be the solution? i declared N as an integer (starts from 0) and it increase by +1
  Dim file As String
            Dim N As Integer = 0
            For Each file In OpenFileDialog1.FileNames

                Try

                    ListBox1.Items.Add(file)

                    Dim myUri As New Uri("ftp://testing.com/public_html/" & OpenFileDialog1.SafeFileNames(N))
                    myFtpUploadWebClient.Credentials = New System.Net.NetworkCredential("", "")

                    'Now you do the uploading
                    myFtpUploadWebClient.UploadFileAsync(myUri, OpenFileDialog1.FileNames(N))


                Catch SecEx As SecurityException
                    ' The user lacks appropriate permissions to read files, discover paths, etc.
                    MessageBox.Show("Security error. Please contact your administrator for details.\n\n" & _
                        "Error message: " & SecEx.Message & "\n\n" & _
                        "Details (send to Support):\n\n" & SecEx.StackTrace)
                Catch ex As Exception
                    ' Could not load the File - probably permissions-related.
                    MessageBox.Show(("Cannot Select File: " & file.Substring(file.LastIndexOf("\"c)) & _
                    ". You may not have permission to read the file, or " + "it may be corrupt." _
                    & ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
                End Try
                N = N + 1
            Next file
        End If


This post has been edited by Bluezap: 30 August 2012 - 12:55 AM

Was This Post Helpful? 0
  • +
  • -

#11 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 31 August 2012 - 12:05 PM

? anyone knows what seems to be wrong here?
Was This Post Helpful? 0
  • +
  • -

#12 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 31 August 2012 - 12:16 PM

What is the problem? Are you getting error? Where? Put the break point to your 3th line where you have For Each file In OpenFileDialog1.FileNames and step through code by pressing F8 on keyboard. Observe what values does variables get when line gets executed...
Was This Post Helpful? 1
  • +
  • -

#13 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 01 September 2012 - 01:23 AM

View Postlucky3, on 31 August 2012 - 12:16 PM, said:

What is the problem? Are you getting error? Where? Put the break point to your 3th line where you have For Each file In OpenFileDialog1.FileNames and step through code by pressing F8 on keyboard. Observe what values does variables get when line gets executed...

I did and when i select the files the error
Webclient does not support concurrent I/O operations
appears for each file selected
And when i check my server to see if the files have got uploaded only the file with index (0) has been uploaded

This post has been edited by Bluezap: 01 September 2012 - 01:23 AM

Was This Post Helpful? 0
  • +
  • -

#14 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Upload Multiple files to a ftp server

Posted 01 September 2012 - 01:42 PM

So you found out, that you can't assign the same instance of WebClient, to deal with more than one file at once.

OK. By now you have:
a) code for uploading one single file to ftp asynchronously
ß) code to loop through all selected files with OpenFileDialog, and prepare all the information needed to upload each of those

What you have to do next is to upload each file with new instance of WebClient (not myFtpUploadWebClient!). Do that in For Each file In OpenFileDialog1.FileNames loop, and don't bother with events of those webclients for now. Just try to upload each selected file to ftp.

This post has been edited by lucky3: 01 September 2012 - 01:44 PM

Was This Post Helpful? 1
  • +
  • -

#15 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 323
  • Joined: 19-January 12

Re: Upload Multiple files to a ftp server

Posted 01 September 2012 - 10:35 PM

View Postlucky3, on 01 September 2012 - 01:42 PM, said:

So you found out, that you can't assign the same instance of WebClient, to deal with more than one file at once.

OK. By now you have:
a) code for uploading one single file to ftp asynchronously
) code to loop through all selected files with OpenFileDialog, and prepare all the information needed to upload each of those

What you have to do next is to upload each file with new instance of WebClient (not myFtpUploadWebClient!). Do that in For Each file In OpenFileDialog1.FileNames loop, and don't bother with events of those webclients for now. Just try to upload each selected file to ftp.

Im sorry i don't seem to understand? can you elaborate :/ this is frustrating :(
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »