Uploading a file using FTP
How to use the libURL commands to upload a file via FTP to a web server.
By Ben Beaumont
We told you last time about our new Knowledgebase site, where you can go and find out answers to all sorts of questions about how to do specific things in Rev. Here is an example of the lessons already posted there, you can check it out for yourself at lessons.runrev.com.
Thank you very much to all the volunteers who have jumped in and started posting lessons at this site!
Create a stack with a button and field
We start by creating a stack and dragging on a button for our script and a field to display our progress and results.
Select a file for upload
We start by selecting a local file that we want to upload. This is done using the 'answer file' command.
on mouseUp
# Start by getting the file to upload
local tFileForUpload
answer file "Select a file to upload"
put it into tFileForUpload
end mouseUp
The second step is the extract the name of the file we wish to upload from the full path. We do this using the itemdelimiter property.
on mouseUp
# Start by getting the file to upload
local tFileForUpload
answer file "Select a file to upload"
put it into tFileForUpload
# Get the name of the file for upload
local tFileName
set the itemdel to "/"
put the last item of tFileForUpload into tFileName
end mouseUp
Setup the FTP connection string
In this example we are going to use the libURLftpUploadFile command. It takes the following 3 parameters:
1) Path the the file you want to upload
example: /filepath/test.txt
2) FTP connection string with destination file path
structure: ftp://username:password@host/filepath
example:ftp://ben:abcdefg@ftp.ben.on-rev.com/public_html/test.txt
3) A callback handler
This is a handler in your script that will be called with a status message when libURL has finished.
example: uploadComplete
While this does sound a little complicated it is quite easy when you see the final code:
constant FTPHOST = "ftp.ben.on-rev.com"
constant FTPUSER = "ben"
constant FTPPASS = "abcdefg"
on mouseUp
# Start by getting the file to upload
local tFileForUpload, tFileName
answer file "Select a file to upload"
put it into tFileForUpload
# Get the name of the file for upload
set the itemdel to "/"
put the last item of tFileForUpload into tFileName
put empty into field 1
# Connect the start the upload
local tDestination
put "ftp://" & FTPUSER & ":" & FTPPASS & "@" & "ftp.ben.on-rev.com/public_html/" & tFileName into tDestination
libURLftpUploadFile tFileForUpload, tDestination, "uploadComplete"
end mouseUp
on uploadComplete pURL, pStatus
put "Status Update:" && pStatus && return after field 1
end uploadComplete
I have defined my connection information at the top of my script in constants. I use them to build the required connection string.
Add Progress
The final step is to add progress updates to your upload. When uploading large files you will want to know how the upload is going. Set the libURLSetStatusCallback before staring your upload to receive messages during upload.
1) You can see the progress message showing how much of the total file has been uploaded which you could use to create a nice visual progress bar.
Here is the final script:
constant FTPHOST = "ftp.ben.on-rev.com"
constant FTPUSER = "ben"
constant FTPPASS = "abcdefg"
on mouseUp
# Start by getting the file to upload
local tFileForUpload, tFileName
answer file "Select a file to upload"
put it into tFileForUpload
# Get the name of the file for upload
set the itemdel to "/"
put the last item of tFileForUpload into tFileName
put empty into field 1
# Connect the start the upload
local tDestination
put "ftp://" & FTPUSER & ":" & FTPPASS & "@" & "ftp.ben.on-rev.com/public_html/" & tFileName into tDestination
libURLSetStatusCallback "uploadProgress", the long ID of me
libURLftpUploadFile tFileForUpload, tDestination, "uploadComplete"
end mouseUp
on uploadComplete pURL, pStatus
put "Status Update:" && pStatus && return after field 1
end uploadComplete
on uploadProgress pURL, pStatus
put "Status Update:" && pStatus && return before field 1
end uploadProgress
|
About the Author
Ben Beaumont is Product Manager for RunRev Ltd.
|
|