Progress of a download

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
dbailey
Posts: 23
Joined: Fri Sep 06, 2013 4:47 pm

Progress of a download

Post by dbailey » Wed Oct 02, 2013 2:59 pm

Hi All,

I'm follow one of the lessons here:- http://lessons.runrev.com/s/lessons/m/4 ... a-download

Just want to know how to change it so the user can be prompted to enter a download location of the file.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Progress of a download

Post by Klaus » Thu Oct 03, 2013 11:49 am

Hi dbailey,

simply replace the automatic saving to disk with an ask file dialog:

Code: Select all

command downloadComplete pURL, pStatus
    ## this is the handler called when the download is finished
    ## the pStatus variable will show the result
    
    ## since the download is complete, there is no need to leave the progress bar visible
    hide scrollbar "ProgressBar"
    
    ## check if there was a problem with the download
    if pStatus = "error" or pStatus = "timeout" then
        answer error "The file could not be downloaded."
    else

        ##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        ## Ask user where to save the file, with our proposal -> desktop
        ask file "Where to save the file:" with ((specialfolderpath("desktop") & "/a_pdf_file.pdf")

        ## IT contains the users choice:
        put URL pURL into URL ("binfile:" & IT)
         ##!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        ## open the downloaded document in the default PDF viewer just to show that it worked
        launch document tFileName
        
        ## to save memory, now unload the URL from memory
        ## this also means that if you want to run the test again, it does not just use a cached version
        unload pURL
    end if
end downloadComplete
Best

Klaus

dbailey
Posts: 23
Joined: Fri Sep 06, 2013 4:47 pm

Re: Progress of a download

Post by dbailey » Thu Oct 03, 2013 12:04 pm

Thanks again Klaus! :D

boysky76
Posts: 48
Joined: Tue Aug 13, 2013 9:49 am

Re: Progress of a download

Post by boysky76 » Wed Oct 30, 2013 11:31 am

Hi,

I ask 2 information, if I use this code with large file size (400mb) the file you download up to about 350mb then stops, as it is possible to change this? can be managed with LiveCode downloading large files.
Is possible to block the download and resume it later?

Thank you

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Progress of a download

Post by Klaus » Wed Oct 30, 2013 2:32 pm

Hi boysky76,
boysky76 wrote:Hi,

I ask 2 information, if I use this code with large file size (400mb) the file you download up to about 350mb then stops, as it is possible to change this? can be managed with LiveCode downloading large files.
never tried this, but it SHOULD work!
boysky76 wrote:Is possible to block the download and resume it later?
No, this is not possible with Livecode.

Hint:
Better create a new topic next time than using and old and more or less "closed" thread! :D


Best

Klaus

boysky76
Posts: 48
Joined: Tue Aug 13, 2013 9:49 am

Re: Progress of a download

Post by boysky76 » Wed Oct 30, 2013 2:39 pm

ok sorry, next time I'll do that ...;), but downloading large files crashes like above (you have any suggestions), is a big limitation that you can not lock the file download in progress.

thanx

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Progress of a download

Post by Klaus » Wed Oct 30, 2013 3:26 pm

HI,

well, it should work 8) , sorry, no idea for a workaround.
I suggest you report this "inconvenience" to: support@runrev.com


Best

Klaus

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Progress of a download

Post by FourthWorld » Wed Oct 30, 2013 4:56 pm

At 400MBs it's probably just running out of memory.

Try changing this:

Code: Select all

put URL pURL into URL ("binfile:" & IT)
To this:

Code: Select all

libURLDownloadToFile pURL, ("binfile:" & IT),"DownloadComplete"
...where "DownloadComplete is the name of a handler which will be triggered when the download has completed, where you can clean up the progress bar or do any other stuff you need to do after successful completion.

With "put URL..." the entire contents are first stored in RAM and then written to disk only after completed, but with libURLDownloadToFile the download happens in chunks that are written to the local file periodically, so that relatively little of the data is in memory at a any given time.

Details on libURLDownloadToFile are in the Dictonary:
http://livecode.com/developers/api/6.0. ... oadToFile/
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Progress of a download

Post by Klaus » Wed Oct 30, 2013 5:21 pm

Oh, yes, I completely overlook that he was using:
put url into url(...)
Yes, that will make the memory blast :D

"liburldownloadtofile" will only load little pieces of the big file at a time!

boysky76
Posts: 48
Joined: Tue Aug 13, 2013 9:49 am

Re: Progress of a download

Post by boysky76 » Wed Oct 30, 2013 9:49 pm

so the code seems to work, but the file is not saved even if it seems downloaded, and if I try to download it another time tells me that the file can not be downloaded as if the pstatus surrendered by an error.
What could be wrong?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Progress of a download

Post by FourthWorld » Wed Oct 30, 2013 9:59 pm

Here's the example given in an older set of docs at http://forums.runrev.com/posting.php?mo ... =7&t=17344

Code: Select all

on mouseUp
         ask file "Select a file to save to"
         if it is not empty then
           put it into tDestPath
           put "http://www.xxxxxx.com/stuff/greatstuff.jpg" into tUrl
           libUrlDownloadToFile tUrl, tDestPath, "loadDone"
         end if
 end mouseUp

on loadDone pUrl, pStatus
          if pStatus is not "cached" then
            answer "Download failed"
          end if
          unload url pUrl
 end loadDone
How are you checking status? Or more specifically, are you checking the data in the params passed to the callback?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

boysky76
Posts: 48
Joined: Tue Aug 13, 2013 9:49 am

Re: Progress of a download

Post by boysky76 » Mon Nov 04, 2013 11:10 am

Hello, thanks for the reply, your advice I have been helpful, now the download is done, only now there's another problem, I would that the file name was put automatically as already present, only the name is in httpheders and I can see it and copy it only by calling the function libUrlLastRHHeaders after downloading the file, I wish I could extrapolate first and use it in variable tname, there is a way to chiamre this url without starting the download to retrieve only the headers?

Post Reply