This sample shows how to upload a file to an FTP server.
using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm"); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader("testfile.txt"); byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); } } } }
This example requires:
-
References to the System.Net namespace.
The example given on this page has an extra closing bracket on line 34. Remove it and then you can convert this example to VB.NET, as I have done below:
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
Public Class WebRequestGetExample
Public Shared Sub Main()
' Get the object used to communicate with the server.
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://www.contoso.com/test.htm"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
' This example assumes the FTP site uses anonymous logon.
request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")
' Copy the contents of the file to the request stream.
Dim sourceStream As New StreamReader("testfile.txt")
Dim fileContents As Byte() = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
sourceStream.Close()
request.ContentLength = fileContents.Length
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(fileContents, 0, fileContents.Length)
requestStream.Close()
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)
response.Close()
End Sub
End Class
End Namespace
This code is very useful. Here are some tips to help you understand it, because there are things which are not so obvious.
1. When you use
FtpWebRequest UploadFileRequest = FtpWebRequest.Create(ftp://www.contoso.com/test.htm) asFtpWebRequest;
When you place the name "test.htm", this is the actual name of the file you want to upload, which is really 'creating' an empty file with that name. I had to upload an empty file. So I just put the name of the file I wanted to create in the folder I wanted it to be.
So if you want it in subfolder "folder3" You can write: Ftp://www.contoso.com/folder1/folder2/folder3/test.htm.
The file will be empty. So then you can either write into the file or place the content of another file: in the example above it would be "testfile.txt". You would obviously have to place a path here to get the file.
The line :
request.ContentLength = fileContents.Length;could be removed from the example.
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.contentlength.aspx
"The ContentLength property is provided only for compatibility with other implementations of the WebRequest and WebResponse classes. There is no reason to use ContentLength."
This encoding method will work for text that is encoded utf-8 but if you want to do any file you should use:
byte[] fileContents = file.ReadAllBytes("testfile.txt");
NOT:
StreamReader sourceStream = new StreamReader("testfile.txt");
byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());