Uploading, Downloading and More


I’ve always wanted some commands in the Visual FoxPro language that would make uploading and downloading files from websites, FTP sites, and file locations extremely easy. Oh, and I also wanted progress indication for these uploads and downloads to be easy to implement as well. Having mucked around with various class libraries and 3rd Party components for years, I decided it was time to write an FLL that did exactly what I wanted so I created vfpconnection.fll.


VFPConnection FLL


Internally vfpconnection.fll is using the LibCurl library (here’s their license), so there are tons of other features I can, and will, add to this as it moves forward. For now it provides VFP developers with what I believe are some of the simplest and easiest file upload and download functions for Visual FoxPro (FTPGet, HTTPGet, FileGet, FTPPut, HTTPPut, FilePut, FTPToStr, and HTTPToStr). It also provides the ability to download files from FTP and HTTP in memory (similar to VFP’s FileToStr function) which avoids disk IO all together and can prove quite convenient when a developer just wants to read something off of a server. And, it provides an extremely slick way of implementing progress indication via a callback function or method.


Next Up …


I created a pretty cool example of how useful having an FLL like this can be. In my next blog entry I’ll present a VFP application I created called AppUpdate which you’ll be able to us as a pretty advanced (though still simple) Loader/Updater for your Visual FoxPro applications. Special thanks ahead of time go out to a client of mine that is allowing me to share it with my readers. In any event, until next time… Visual FoxPro Rocks!


Here’s the customary download link, some sample code for using the vfpconnection.fll, and the library documentation…







PLEASE NOTE: An updated version of this FLL and documenation is available here.







VFP Connection FLL:


VFPConnection FLL Download (90 KB approx.)



VFP Connection Sample Code:


SET LIBRARY TO (LOCFILE(“vfpconnection.fll”,“FLL”))


?FTPGet(“FTP://myusername:mypassword@mysite.com/mydir/myfile.zip”, “C:\myfile.zip”, “MyCommand()”)


?HTTPGet(“http://www.mysite.com/mypage.htm”, “C:\mypage.htm”, “MyCommand()”)


?FILEGet(“File://C:\MyFile.txt”, “C:\MyFileCopy.txt”, “MyCommand()”)


?FTPPut(“C:\myfile.zip”, “FTP://myusername:mypassword@mysite.com/mydir/myfile.zip”, “MyCommand()”)


?HTTPPut(“C:\mypage.htm”, “http://www.mysite.com/mypage.htm”, “MyCommand()”)


?FilePut(“C:\myfile.zip”, “File://C:\myfile.zip”, “MyCommand()”)


m.lcString1 = FTPToStr(“FTP://myusername:mypassword@mysite.com/mydir/myfile.txt”, “MyCommand()”)


m.lcString2 = HTTPToStr(“http://www.mysite.com/mypage.htm”, “MyCommand()”)


SET LIBRARY TO

FUNCTION MyCommand()
    ?nConnectTotalBytes
    ?nConnectBytesSoFar
ENDFUNC


 


VFP Connection Documenation:


Function FTPGet()


Signature: FTPGet(cSourceURL, cDestination[, cCallbackFunction)


Parameters:


cSourceURL – The URL to the file you wish to download.


cDestination – The full path and file name where you want the source file saved.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Logical – returns .T. if successful or .F. if the operation has failed.


Remarks:


This function provides the ability to download a file from an FTP site.


In order to specify port or login information you would simply include them in cSourceURL, such as “FTP://myusername:mypassword@myftpsite.com:21/mydir/myfile.zip”.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).






Function HTTPGet()


Signature: HTTPGet(cSourceURL, cDestination[, cCallbackFunction)


Parameters:


cSourceURL – The URL to the file you wish to download.


cDestination – The full path and file name where you want the source file saved.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Logical – returns .T. if successful or .F. if the operation has failed.


Remarks:


This function provides the ability to download a file from a website.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).






Function FILEGet()


Signature: FILEGet(cSourceURL, cDestination[, cCallbackFunction])


Parameters:


cSourceURL – The full path and file name to the file you wish to copy.


cDestination – The full path and file name where you want the source file saved.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Logical – returns .T. if successful or .F. if the operation has failed.


Remarks:


This function provides the ability to copy a file from a local or remote location.


The format for the cSourceURL parameter is slightly different than you might expect. In order to copy file C:\MyDir\MyFile.zip you would specify cSourceURL as  “FILE:// C:\MyDir\MyFile.zip”.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).






Function FTPPut()


Signature: FTPGet(cSource, cDestinationURL[, cCallbackFunction)


Parameters:


cSource – The full path and file name of the file you want uploaded.


cDestinationURL – The URL to where the file specified in cSource should be uploaded to.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Logical – returns .T. if successful or .F. if the operation has failed.


Remarks:


This function provides the ability to upload a file to an FTP site.


In order to specify port or login information you would simply include them in cDestinationURL, such as “FTP://myusername:mypassword@myftpsite.com:21/mydir/myfile.zip”.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).






Function HTTPPut()


Signature: HTTPGet(cSource, cDestinationURL[, cCallbackFunction)


Parameters:


cSource – The full path and file name of the file you want uploaded.


cDestinationURL – The URL to where the file specified in cSource should be uploaded to.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Logical – returns .T. if successful or .F. if the operation has failed.


Remarks:


This function provides the ability to upload a file to a website.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).






Function FILEPut()


Signature: FILEPut(cSource, cDestinationURL[, cCallbackFunction])


Parameters:


cSource – The full path and file name of the file you want copied.


cDestinationURL – The full path and file name where the file specified in cSource should be copied to.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Logical – returns .T. if successful or .F. if the operation has failed.


Remarks:


This function provides the ability to copy a file to a local or remote location.


The format for the cDestinationURL parameter is slightly different than you might expect. In order to copy file C:\MyDir\MyFile.zip you would specify cDestinationURL as  “FILE:// C:\MyDir\MyFile.zip”.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).






Function FTPToStr()


Signature: FTPToStr(cSourceURL[, cCallbackFunction)


Parameters:


cSourceURL – The URL to the file you wish to return a string from.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Character Data – the contents of the file specified by cSourceURL


Remarks:


This function provides the ability to retrieve a file from an FTP site as a string.


In order to specify port or login information you would simply include them in cSourceURL, such as “FTP://myusername:mypassword@myftpsite.com:21/mydir/myfile.txt”.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).






Function HTTPToStr()


Signature: HTTPToStr(cSourceURL[, cCallbackFunction)


Parameters:


cSourceURL – The URL to the file you wish to download.


cCallbackFunction – An optional string denoting a function, procedure, or method that you want fired whenever a read/write occurs, such as “MyCallback()”.


Return Value:


Logical – returns .T. if successful or .F. if the operation has failed.


Remarks:


This function provides the ability to retrieve a file from a website as a string.


When the cCallbackFunction is called 2 variables (nConnectTotalBytes and nConnectBytesSoFar) are created dynamically by the FLL. These variables can be used within the specified function, procedure, or method to determine the total size of the source file in bytes as well as the total progress of the operation (Percentage = 100 * nConnectBytesSoFar / nConnectTotalBytes).