Uploading File (Error)

Tantie

Member
Joined
Sep 3, 2008
Messages
9
Programming Experience
Beginner
Hello,

I'm trying to send a file from a local program (vb.net) to a webserver. I'm using the file on the website. But I keep having an error:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.

Here is the code I'm using:
VB.NET:
    Public Sub Upload()

        Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("FTP file location"), System.Net.FtpWebRequest)
        Dim bFile() As Byte = System.IO.File.ReadAllBytes("File location")
        Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()

        Try
            clsRequest.Credentials = New System.Net.NetworkCredential("Username", "Password") 'ftp username and password van de site
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
        Catch ex As Exception
            MessageBox.Show("Fault", "FOUTMELDING", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

Can somebody help me?

Thank you
 
From help for GetRequestStream:
Set the request properties before calling the GetRequestStream method.
You have to set Credentials and Method before calling GetRequestStream.
 
What you do now:
VB.NET:
.GetRequestStream()
.Credentials = 
.Method =
What help says you must do:
VB.NET:
.Credentials = 
.Method =
.GetRequestStream()
 
Now I'm having the error, "The remote server returned an error: (550) File unavailable".

I used already another FTP (WS-FTP) and send the file, ok. But in the program?????

Any help??

Here's my code:

VB.NET:
Try
   Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("FTP adress + File.txt"), System.Net.FtpWebRequest) 
            Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\File.txt") 
            Dim clsStream As System.IO.Stream 
            clsRequest.Credentials = New System.Net.NetworkCredential("USERNAME", "PASSWORD")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            clsStream = clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
        Catch ex As Exception
            MessageBox.Show("Fault: " + ex.Message, "FOUTMELDING", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Will the code work (if the problem is fixed) on *.mdb files??????

thank you.
 
With correct request URI that should work. You can also use the My.Computer.Network.UploadFile method.
 
Back
Top