Problems with Upload

Mar 5, 2013 at 8:47 AM
Hey,

I managed to get Downloads to work with the following code:
        Private Sub Download(ByVal filename As String, ByVal localpath As String)
        Using FtpClient As New FtpClient
            FtpClient.Host = ftpHost
            FtpClient.Credentials = credentials
            FtpClient.EncryptionMode = FtpEncryptionMode.Explicit
            AddHandler FtpClient.ValidateCertificate, AddressOf MyHandlerMethod

            Using istream As Stream = FtpClient.OpenRead(filename)
                Dim fileoutput As New FileStream(localpath, FileMode.Create, System.IO.FileAccess.ReadWrite)

                Try

                    Dim buffer As Byte() = New Byte(8 * 1024 - 1) {}
                    Dim len As Integer
                    While (InlineAssignHelper(len, istream.Read(buffer, 0, buffer.Length))) > 0
                        fileoutput.Write(buffer, 0, len)
                    End While
                Finally

                    fileoutput.Close()
                    istream.Close()
                End Try
            End Using
        End Using

    End Sub
I tried to turn this around to get my Upload-method:
       Private Sub Upload(ByVal filename As String, ByVal remotepath As String)
        Using FtpClient As New FtpClient
            FtpClient.Host = ftpHost
            FtpClient.Credentials = credentials
            FtpClient.EncryptionMode = FtpEncryptionMode.Explicit
            AddHandler FtpClient.ValidateCertificate, AddressOf MyHandlerMethod

            Using istream As Stream = FtpClient.OpenWrite(remotepath)
                Dim fileoutput As New FileStream(filename, FileMode.Open, System.IO.FileAccess.Read)

                Try

                    Dim buffer As Byte() = New Byte(8 * 1024 - 1) {}
                    Dim len As Integer
                    While (InlineAssignHelper(len, istream.Read(buffer, 0, buffer.Length))) > 0
                        fileoutput.Read(buffer, 0, len)
                    End While
                Finally

                    fileoutput.Close()
                    istream.Close()
                End Try
            End Using
        End Using

    End Sub
I get an unhandled Exception in Line:
                    While (InlineAssignHelper(len, istream.Read(buffer, 0, buffer.Length))) > 0
Yeah I know this is not C# :D

Best regards
Coordinator
Mar 5, 2013 at 1:04 PM
Streams returned from OpenWrite() and OpenAppend() are meant to be written to, not read. Your variable naming is a bit confusing in regards to your intents, I'm assuming based on what you said and that you called the OpenWrite() method that you ware wanting to perform an upload. You need to Read() the local file stream and Write() to the stream returned from OpenWrite().
Coordinator
Mar 5, 2013 at 1:06 PM
There are examples for downloads and uploads in the source tree. There is also a CHM help document that includes examples for most of the methods in FtpClient. You should read over the examples I took the time to write for precisely this reason. If you're finding that the included examples are inadequate please point that out so that I can improve the documentation.
Mar 5, 2013 at 1:18 PM
I haven't seen a help document in the source tree here. I still can't find it even now.

by the way i could fix the uploading myself after some time.
Coordinator
Mar 5, 2013 at 1:28 PM
Download the latest release, help document is there. Examples are in the Examples directory on the source tree which you can browse in the Source Code section of this site.
Coordinator
Mar 5, 2013 at 1:34 PM
Here is the OpenWrite example. You read and write the streams just like any other stream based operation except the streams returned by FtpClient are not bi-directional. You can't read and write, you can do one or the other depending on the method you call. OpenWrite() returns a stream that is writeable. To upload a file you need to open the local file for reading and call OpenWrite() to get a stream to write the file contents to. You loop + Read() the local file stream and Write() the bytes read to the stream OpenWrite() returned. It's a pretty straight forward, simple and standard stream operation.
  while((read = localFile.Read(buf, 0, buf.length)) > 0) {
      remoteFile.Write(buf, 0, read);
  }