mobile download file

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: Klaus, FourthWorld, heatherlaine, kevinmiller

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

mobile download file

Post by vedus » Fri Nov 14, 2014 2:00 pm

i am using the bellow code to download a file from my ftp server.
On the mac desktop is working ok without problems,but in the apple simulator and in the android (phone Z1) i don't get anything..
No messages,no download,nothing and the field and progress bar don't show up.

Code: Select all

constant FTPHOST = "myip"
constant FTPUSER = "user"
constant FTPPASS = "pass"
global sDownloadStart 
on mouseUp
    put "data.sqlite" into tFileName
    put "ftp://" & FTPUSER & ":" & FTPPASS & "@" & FTPHOST & "/" & tFileName into myUrl
    -- set up a message to show the status of the download as it progresses
    libURLSetStatusCallback "showProgress", the long name of me
    -- make sure the progress bar is hidden, as this property is used to initialise it
    hide scrollbar "ProgressBar"
    put empty into fld "ProgressField"
    put the seconds into sDownloadStart
    -- start the download process, telling Rev to call the "downloadComplete" handler when finished
    load URL myUrl with message "downloadComplete"
end mouseUp

command showProgress pURL, pStatus
   -- this is the status callback message which gets called regularly during the download
   -- pStatus will show some initialisation messages, then
   -- loading,bytesReceived,bytesTotal
   -- using a graphical progress bar instead
   -- ths first time this is called, find the total number of bytes that are to be downloaded
   -- use this info to set the span of the progress bar
   	
   -- wait until the download info is being received
   if the number of items in pStatus = 3 then
      if the visible of scrollbar "Progressbar" = false then
         put the last item of pStatus into tTotalBytes
         set the startValue of scrollbar "Progressbar" to 0
         set the endValue of scrollbar "Progressbar" to tTotalBytes
         show scrollbar "Progressbar"
      end if
      
      set the thumbPosition of scrollbar "Progressbar" to item 2 of pStatus
   end if
   
   -- better text information
   if the number of items in pStatus = 3 then
      put item 2 of pStatus into tBytesReceived
      put item 3 of pStatus into tTotalBytes
      
      -- this gives very large numbers that are not easily read, so convert to KB
      put tBytesReceived div 1024 into tKBreceived
      put tTotalBytes div 1024 into tTotalKB
      
      -- calculate speed
      put the seconds - sDownloadStart into tElapsedSeconds
      if tElapsedSeconds = 0 then
         -- make sure we don't divide by zero at the start
         put "unknown" into tKBperSecond
      else
         put round(tKBreceived / tElapsedSeconds, 1) into tKBperSecond
      end if
      
      --put "Received " & tKBreceived & " KB of " & tTotalKB & " KB at " into fld "ProgressField"
      put "Received " & tKBreceived  into fld "ProgressField"
      //put tKBperSecond & " KB/sec" after fld "ProgressField"
   end if
end showProgress
 
   
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
        -- work out a file name for saving this file to the desktop
        set the itemDel to slash
        put "binfile:" & specialfolderpath("documents") & "/data.sqlite" into myPath
        put url purl into url myPath
        answer "Download completed" with ok
        put empty into fld "ProgressField"
        dispatch "ResetList" to group "datagrid1" of group "scroller1" of card "base"
        
        -- 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

Klaus
Posts: 11488
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: mobile download file

Post by Klaus » Fri Nov 14, 2014 2:08 pm

Hi vedus,

according to the dictionary, which I always consult if in doubt 8) ,
"libURLSetStatusCallback", like most of the LIBURL stuff, is NOT supported
on the mobile platform! So your script will silently fail at this point.


Best

Klaus

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

Re: mobile download file

Post by vedus » Fri Nov 14, 2014 2:52 pm

Klaus wrote:Hi vedus,

according to the dictionary, which I always consult if in doubt 8) ,
"libURLSetStatusCallback", like most of the LIBURL stuff, is NOT supported
on the mobile platform! So your script will silently fail at this point.


Best

Klaus
thx klaus
is there any alternate method to show up the progress bar or the KB/s that have downloaded?

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2423
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: mobile download file

Post by jmburnod » Fri Nov 14, 2014 5:56 pm

Hi vedus,

I think urlProgress is the magic word for this job on mobile
Best regards
Jean-Marc

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

Re: mobile download file

Post by vedus » Sat Nov 15, 2014 8:21 pm

i have reproduce my code and i have success with progress bar and field with total Kb/s downloaded.
the only problem i have is i don't get the answer that file have by downloaded.
Can someone tell me where i do wrong?
here is the code.

Code: Select all

constant FTPHOST = "myIP"
constant FTPUSER = "user"
constant FTPPASS = "pass"
global tFileName
on mouseUp
    set the cProgress of group "prbar1" of group "up_bar" to 0
    put "test.zip" into tFileName
    put "ftp://" & FTPUSER & ":" & FTPPASS & "@" & FTPHOST & "/" & tFileName into pUrl
    -- start the download process, telling Rev to call the "downloadComplete" handler when finished
    Load URL pUrl with message "urlProgress"

    
end mouseUp

on urlProgress pURL, pStatus, pBytesDone, pBytesTotal
    put pStatus into fld "pf1" of group "up_bar"
    switch item 1 of pStatus

              case "loading"
            set the cProgress of group "prbar1" to round((pBytesDone / pBytesTotal) * 100)
            put the round of (item 1 of pBytesDone / 1024)&& "KB Downloaded" into field "status"
            break
        case "cached"
            -- work out a file name for saving this file to the desktop
            set the itemDel to slash
            put "binfile:" & specialfolderpath("documents") & "/test.zip" into myPath
            put url pUrl into url myPath
            answer "Download completed" with ok
            -- 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
            
                     break
              case "error"
                   if pStatus = "error" or pStatus = "timeout" then
                answer error "The file could not be downloaded." with "ok"
            end if
                     break
                  
       end switch
    
end urlProgress

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2423
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: mobile download file

Post by jmburnod » Sun Nov 16, 2014 1:16 pm

Hi Vedus,
What happen if you put

Code: Select all

case "downloaded"
answer "Download completed"
break
Best regards
Jean-Marc

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

Re: mobile download file

Post by vedus » Sun Nov 23, 2014 2:05 pm

If I put case "download" the result I get is to have double download of the file,and progress bar start over again.

Klaus
Posts: 11488
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: mobile download file

Post by Klaus » Sun Nov 23, 2014 2:09 pm

vedus wrote:If I put case "download" the result I get is to have double download of the file,and progress bar start over again.
Sorry, don't get this, if you do this, then the file will be downloaded twice? :shock:

Code: Select all

...
case "downloaded"
case "cached"
  -- work out a file name for saving this file to the desktop
  set the itemDel to slash
  put "binfile:" & specialfolderpath("documents") & "/test.zip" into myPath
  put url pUrl into url myPath
 answer "Download completed" with ok
...

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

Re: mobile download file

Post by vedus » Sun Nov 23, 2014 9:41 pm

Hi klaus.
I am using a field to show me how is progress,if I have contact the server,if there is loading etc.in the end I get in the field the word downloaded,so there is no problem.
So I go and put the >> case "downloaded" the result is,the file have started and download is on progress,when the file is on the end,the progress bar restart and the file is re-downloaded in the end of second download I get 2 messages that file have be downloaded !!!

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: mobile download file

Post by dave.kilroy » Mon Nov 24, 2014 3:24 am

vedus do you have "break" in-between all your "case" statements? A missing "break" might account for double-downloading...
"...this is not the code you are looking for..."

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

Re: mobile download file

Post by vedus » Mon Nov 24, 2014 6:11 pm

hi Dave,yes i have breaks between cases,but i don't understand why i get re-download when i put the case "downloaded"..
Dictionary say that the messages is
  • contacted - The server has been contacted.
    requested - A request was made to the URL.
    loading - Data is being downloaded.
    uploading - Data is being uploaded to the server.
    downloaded - Data has finished downloading.
    uploaded - Data has finished uploading.
    error - An error occurred.
And i get the bellow
1:) Contacted
2:) Loading
3:) downloaded
i Don't get the >"cached"
So my file is no saved in the folder.
If i change the case "cached" to the "downloaded" my file downloaded over and over without stoping.
i will post again my code.i know somewhere is something wrong,but i don't understand where!!!

Code: Select all

constant FTPHOST = "myIP"
constant FTPUSER = "user"
constant FTPPASS = "pass"

on mouseUp
    set the cProgress of group "prbar1" of group "up_bar" to 0
    put "fms.zip" into tFileName
    put "ftp://" & FTPUSER & ":" & FTPPASS & "@" & FTPHOST & "/" & tFileName into pUrl
    -- start the download process, telling Rev to call the "downloadComplete" handler when finished
    Load URL pUrl with message "urlProgress"


    
end mouseUp

on urlProgress pURL, pStatus, pBytesDone, pBytesTotal
        put pStatus into fld "pf1" of group "up_bar"
        switch pStatus
        
                      case "loading"
                        set the cProgress of group "prbar1" to round((pBytesDone / pBytesTotal) * 100)
                        put the round of (item 1 of pBytesDone / 1024)&& "KB Downloaded" into field "status"
                        break
                      case "error"
                               if pStatus = "error" or pStatus = "timeout" then
                                answer error "The file could not be downloaded." with "ok"
                        end if
                                 break  
          case "downloaded"
            set the itemDel to slash
put specialfolderpath("documents") & "/fms.zip" into myPath
put pUrl into URL ("binfile:" & myPath)
put url pUrl into url myPath
            answer "Update Completed" with "Ok"
                unload pURL
            break
           end switch    
end urlProgress
file is from my ftp server and is 6 MB
Last edited by vedus on Mon Nov 24, 2014 6:50 pm, edited 2 times in total.

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: mobile download file

Post by dave.kilroy » Mon Nov 24, 2014 6:31 pm

Looking at your code it appears that by the time you get to "downloaded" the variable pUrl should contain all the downloaded data and all you need to do is to save it

Code: Select all

 put specialfolderpath("documents") & "/fms.zip" into myPath
put pUrl into URL ("binfile:" & myPath) 
and that if you carry on with

Code: Select all

put url pUrl into url myPath
it will indeed download it all over again - or - am I missing something?

EDIT: left in a bit of bad syntax...now removed
"...this is not the code you are looking for..."

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

Re: mobile download file

Post by vedus » Mon Nov 24, 2014 6:43 pm

dave.kilroy wrote:Looking at your code it appears that by the time you get to "downloaded" the variable pUrl should contain all the downloaded data and all you need to do is to save it

Code: Select all

 put specialfolderpath("documents") & "/fms.zip" into myPath
put pUrl into URL ("binfile:" & myPath) 
and that if you carry on with

Code: Select all

put url pUrl into url myPath
it will indeed download it all over again - or - am I missing something?

EDIT: left in a bit of bad syntax...now removed
i have try this way,and still the file is no saved, and the download go on over and over with progress bar reset and over again..

i have rewrite my code above in my last message!!

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Location: Plymouth, UK
Contact:

Re: mobile download file

Post by dave.kilroy » Mon Nov 24, 2014 7:55 pm

hmm - sorry vedus I'm obviously missing something - I'm sure somebody else will stop by soon and point it out...
"...this is not the code you are looking for..."

vedus
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 153
Joined: Tue Feb 26, 2013 9:23 am

Re: mobile download file

Post by vedus » Mon Nov 24, 2014 8:11 pm

dave.kilroy wrote:hmm - sorry vedus I'm obviously missing something - I'm sure somebody else will stop by soon and point it out...
no problem Dave thx for the help anyway ;)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”