The question, “How can I FTP a binary file with AVR for .NET” has come up a time or two in tech support lately. Here is a quick and easy response to that question. As you’ll see, uploading a file with FTP with AVR for .NET is pretty simple.
The code in Figure 1 shows how to call the function, FtpUploadFile, that this article provides. FtpUploadFile needs five arguments:
- The URI of the target FTP site (including any folders)
- The target name of the file on the FTP server
- The fully qualified path of the file to upload
- The FTP login name
- The FTP login password
In the example in Figure 1, the FTP server is at ftp://ftp.myftpserver.com and the folder for the uploaded file is “spreadsheets.” These two values comprise the first argument, with a slash (“/”) added between them. This argument can optionally end with a trailing slash (“/”). In this example, the status value returned is either the text returned from the FTP operation or an exception message.
DclFld status Type(*String)
status = FtpUploadFile("ftp://ftp.myftpserver.com/spreadsheets",+
"neil", "young" )
Figure 1. Uploading a file with FTP
The meat of this article is below in Figure 2. This is the function that does the heavy lifting of uploading a file with FTP.
BegFunc FtpUploadFile String Access(*Public)
DclSrParm ftpUri Type(*String)
DclSrParm targetFileName Type(*String)
DclSrParm sourceFilePath Type(*String)
DclSrParm user Type(*String)
DclSrParm pwd Type(*String)
DclFld request Type(FtpWebRequest)
DclFld response Type(FtpWebResponse)
DclFld requestStream Type(Stream)
DclFld status Type(String)
DclArray fileContents Type(Byte) Rank(1)
If (NOT ftpUri.EndsWith("/"))
ftpUri = ftpUri + "/"
request = WebRequest.Create(ftpUri + TargetFileName) *As FtpWebRequest
request.Method = WebRequestMethods.Ftp.UploadFile
request.UseBinary = *True
request.Credentials = *New NetworkCredential(user, pwd)
fileContents = File.ReadAllBytes(SourceFilePath)
request.ContentLength = fileContents.Length
requestStream = request.GetRequestStream()
requestStream.Write(fileContents, 0, fileContents.Length)
response = request.GetResponse() *As FtpWebResponse
status = response.StatusDescription
Catch except Exception
status = "Exception occured: " + except.Message
Figure 2. FtpUploadFile function
At the core of the FtpUploadFunction are two objects in the System.Net namespace:
These methods were added with the .NET Framework version 2.0. The code shown here is for .NET Framework 4.0.
Here is a quick rundown of the major parts of FtpUploadFile:
Ensure that the FtpUri ends with a slash (“/”) and then append the target file name (the name you want the uploaded file to have on the target computer).
Set the properties of the request instance of the FtpWebRequest object. In this case we’re setting the object to upload binary files and specifying the FTP login credentials.
Read all bytes of the source file and into the fileContents byte array.
Instance a requestStream (of type Stream) and write the contents of the fileContents byte array to it.
Invoke the upload, save its StatusDescription in the status variable, and close the response instance.
If any exception was thrown, save the exception message to the status variable. For production work you might want to return an instance of a class with more information than this.
Check out of this motel! You’re done.
That’s all there is to it. It’s short, sweet, and works like a champ!