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.
0001 Using System 0002 Using System.Text 0003 Using System.Net 0004 Using System.IO 0005 ... 0006 DclFld status Type(*String) 0007 0008 status = FtpUploadFile("ftp://ftp.myftpserver.com/spreadsheets",+ 0009 "sample1.xlsx",+ 0010 "c:\Users\Roger\Documents\sample1.xlsx",+ 0011 "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.
0001 BegFunc FtpUploadFile String Access(*Public) 0002 DclSrParm ftpUri Type(*String) 0003 DclSrParm targetFileName Type(*String) 0004 DclSrParm sourceFilePath Type(*String) 0005 DclSrParm user Type(*String) 0006 DclSrParm pwd Type(*String) 0007 0008 DclFld request Type(FtpWebRequest) 0009 DclFld response Type(FtpWebResponse) 0010 DclFld requestStream Type(Stream) 0011 DclFld status Type(String) 0012 0013 DclArray fileContents Type(Byte) Rank(1) 0014 0015 Try 0016 If (NOT ftpUri.EndsWith("/")) 0017 ftpUri = ftpUri + "/" 0018 Endif 0019 request = WebRequest.Create(ftpUri + TargetFileName) *As FtpWebRequest 0020 request.Method = WebRequestMethods.Ftp.UploadFile 0021 request.UseBinary = *True 0022 request.Credentials = *New NetworkCredential(user, pwd) 0023 0024 fileContents = File.ReadAllBytes(SourceFilePath) 0025 request.ContentLength = fileContents.Length 0026 0027 requestStream = request.GetRequestStream() 0028 requestStream.Write(fileContents, 0, fileContents.Length) 0029 requestStream.Dispose() 0030 0031 response = request.GetResponse() *As FtpWebResponse 0032 status = response.StatusDescription 0033 response.Close() 0034 Catch except Exception 0035 status = "Exception occured: " + except.Message 0036 EndTry 0037 0038 LeaveSr status 0039 EndFunc
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!