FTP upload

HeBTeMy

New member
Joined
Sep 3, 2012
Messages
3
Programming Experience
Beginner
Hello, i need to upload a text that it's on a textbox to ftp,
I have this code

VB.NET:
 Try
            Dim sourceStream As New StreamReader(TextBox1.Text)
            Dim fileContents As Byte() = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
            sourceStream.Close()

            Dim Request As System.Net.FtpWebRequest = FtpWebRequest.Create("ftp://@ftp.drivehq.com/Important.txt")

            Request.Credentials = New NetworkCredential("Username", "Password")
            Request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            Request.Proxy = Nothing
            Request.KeepAlive = False


            Request.GetRequestStream.Write(fileContents, 0, fileContents.Length)
            Request.Abort()

           Catch ex As Exception

        End Try

The application doesn't FC but it gives this on "Immediate Window" everytime it's supposed to send

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll

I don't understand what's the problem...? Is there another way to send it to ftp or what?
 
Here is the FTP code I normally use and work fine.

   Public Function PutFile(ByVal SourceFile As String, ByVal DestinationFile As String) As Boolean
        Dim success As Boolean = True
        Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + _ftpServer + "/" + DestinationFile), System.Net.FtpWebRequest)
        request.Credentials = New System.Net.NetworkCredential(_ftpUser, _ftpPass)
        request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        Dim stream As System.IO.Stream = Nothing
        Try
            Dim file() As Byte = System.IO.File.ReadAllBytes(SourceFile)
            stream = request.GetRequestStream()
            stream.Write(file, 0, file.Length)
        Catch ex As Exception
            success = False
        Finally
            If Not IsNothing(stream) Then
                stream.Close()
                stream.Dispose()
            End If
        End Try
        Return success
    End Function
 
Edited: I found out how it is.. but i have another issue, i have to send Timeofday in the name of the txt, how can i do that? Everytime i try it it gives me: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim URI As String = "ftp://ftp.drivehq.com/Important/" & TimeOfDay
Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)

request.Credentials = New System.Net.NetworkCredential(TextBox2.Text, TextBox3.Text)
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

Dim encoding As New System.Text.UTF8Encoding()
Dim file() As Byte = encoding.GetBytes(TextBox4.Text) 
Dim strz As System.IO.Stream = request.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
End Sub

How can i define the name of the txt? It must be allways equal? I Can't change it to everytime it sends doesn't replace the other one?
 
Last edited:
TimeOfDay needs to be formatted correctly, in it's normal format it contains /'s and :'s which are not allowed in file names. For timestamp issues I usually use something like:

Dim s As String = Now.ToString("yyyyMMdd_HHmmss")     ' 20121018_235213  
 
And just a tip and I try to stress this to everybody. If you are not getting a compile error by adding TimeOfDay to a string you don't have Option Strict On. You are adding a string type to a date type. This can cause issues that can be very hard to debug in large applications. Option Strict won't let you do stuff like this, protects you from other things like functions not returning a value in all paths, etc. In project properties go to Compile Options and turn option strict ON. The only reason VB.NET has it off is so that you can try to convert VB6 applications (or at least that's the only reason I've ever heard). You can also set it in Visual Studio options to have all VB.NET apps use Option Strict On so that all new apps use it. Sorry to get long winded, I'm not telling you how to do anything. I've just seen this first hand and have had to fix a lot of it first hand.

The company I just started working for have apps that I've been trying to make changes to but don't have Option Strict On. If I turn it on there are over errors that keep the app from compiling. I'm going nuts trying to fix some of this stuff. :)


Randy
 
Back
Top