libURLDownloadToFile - can I make it blocking?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
cborn
Posts: 66
Joined: Fri Dec 15, 2006 11:35 pm

libURLDownloadToFile - can I make it blocking?

Post by cborn » Thu Jun 30, 2011 3:20 am

Interestingly, the Dictionary entry for libURLDownloadToFile says:
The callbackMessage is sent to the object whose script contains the libURLDownloadToFile command, after the download is complete, so you can handle the callbackMessage to perform any tasks you want to delay until the file has been downloaded. Two parameters are sent with the message: the URL and the URLStatus of the file.
Unfortunately, this does not seem to be true, libURLDownloadToFile seems to be non-blocking. My problem is that I need it to BE blocking. I need to embed custom headers in the GET command because I'm dealing with a secure server, so I can't just use

Code: Select all

put URL("serverpath") into URL("binfile:/localpath")
However, in my scripts the files don't seem to download fully or at all if the rest of the script continues to run. I've been banging my head against this wall for two days and I can't figure out a way around this problem. Any suggestions? :?

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: libURLDownloadToFile - can I make it blocking?

Post by shaosean » Thu Jun 30, 2011 5:14 am

Correct, it is non-blocking.. The code that is in the callback message is what is blocked until the file is finished downloading..

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: libURLDownloadToFile - can I make it blocking?

Post by SparkOut » Thu Jun 30, 2011 11:32 am

If you set your authentication cookies with the "set the httpHeaders to <myHeaders>" command beforehand, you actually can use

Code: Select all

put URL("serverpath") into URL("binfile:/localpath")
type code. I wouldn't recommend it though, for a file download, especially a large one.

libURLDownloadToFile will be much more robust and you can block it by using the callback function, and/or libURLSetStatusCallback. For instance

Code: Select all

local sDownloadStatus
-- a script local variable to be available to the download handler and the callback scripts

on getDownloadFile

   -- do any preparation stuff like set the httpHeaders

   -- set a callback function to update the status continually
   libURLSetStatusCallback "updateStatus",the long ID of me
  
   -- start the download, and reference the callback function to be called when the download is complete
   libURLDownloadToFile (theServerUrl,tDestinationPath),"downloadComplete"

   wait until sStatus is "Download complete" with messages
end getDownloadFile

on updateStatus pUrl, pStatus
   put pStatus into sStatus
   -- since this is a continually updated status message, we might as well show it
   put sStatus into field "fldStatus"
  -- putting it into a field is a little crude, but if the first item of pStatus is "loading" we could use items 2 and 3 to update a progress bar instead.
end updateStatus

on downloadComplete
   put "Download complete" into sStatus
   put sStatus into field "fldStatus"
   -- once the file is downloaded, cancel the callback function by setting it without parameters
   libURLSetStatusCallback
end downloadComplete
This is a double-whammy. We can make the getDownloadFile blocking by waiting for the downloadComplete callback function to be triggered, which would, on its own, not allow for any progress feedback. We could also make it blocking by using the status callback and waiting for that to contain "downloaded" instead, and have the advantage of being able to display feedback on the download progress. Or almagamate the two, as above, so that you can show progress feedback while the file is being downloaded, and still use the downloadComplete callback (my arbitrary name) to handle the blocking. No special advantage to that (mixing the two), I just did it this way to show you different methods. You do get more information (especially if there is an error) by testing the status callback function results, so you could check for pStatus in the updateStatus handler containing "error" or "timeout" for instance and exit the download handler gracefully.

cborn
Posts: 66
Joined: Fri Dec 15, 2006 11:35 pm

Re: libURLDownloadToFile - can I make it blocking?

Post by cborn » Thu Jun 30, 2011 2:01 pm

OK, I think I get it, but I'm not sure it will work. I thought that I could use the callback message to call the handler of the next set of tasks, but it seems to be calling the callback message long before it downloads the file. I have the libURLDownloadToFile in a repeating loop to download an arbitrary number of small audio files. Once the files are downloaded I have a set of tasks that needs to be done for each file (creating buttons, players etc). However, if I have it try to call the handler that does these tasks, the files do not download. If I bypass the tasks and just focus on the downloads, then I get can get the files to download ONLY if I throw in a 2 second wait to allow the files to finish composing. Else, I get empty files.

I'll try your technique and see if the added status stuff will do the trick, and let you know. Thanks for your help!

Post Reply

Return to “Internet”