4 Replies Latest reply on Jun 9, 2011 11:46 AM by Production Monkey

    Download from internet with lingo

    MECS Level 1

      Can I write lingo that will allow a Director movie to download a file from the internet and put it, say, onto the desktop of the user's computer?  I don't mean in a "stealth" fashion.  I mean a Director movie that the person chooses to run because they want to download that file.


      I tried downloadNetThing, but from the example in the manual, I can't seem to get the syntax sorted out properly, or there is some other problem.  Here's the code I'm currently trying:


      on exitFrame me
        downloadTheNetThing("http://www.mydomain.com\images\myinstaller.exe",c:\Documents and Settings\owner\desktop&"myinstaller")







        • 1. Re: Download from internet with lingo
          Production Monkey Level 3

          Please don't use "exitFrame" for such an action. "MouseUp" or its kind would be a much better choice.


          The following works on my XP system:


          downloadNetThing("http://chart.apis.google.com/chart?cht=p3&chd=t%3a90,10&chs=250x100&chl=Pacman","C:\Documents and Settings\All Users\Desktop\test1.png")



          Note: The colon in the URL has been replaced with its url encoding,  "%3a". I used Director's urlEncode function to get that value.

          • 2. Re: Download from internet with lingo
            MECS Level 1

            Thanks for the response, Pro Mo.


            I tried what you suggested and the program saved the script just fine, so there were apparently no syntax errors in the code I entered.  However, when I ran it and examined netDone, it contained the following <Xtra "NetLingo" 0 22ba88>.  It would appear that a Netlingo Xtra is missing from my xtras folder.  I only find one, and it is in the folder with the Director movie I am working on.


            Here is the code.  Do you see a problem, or does the issue to appear to be a missing xtra?


            on exitFrame me

            netID=downloadNetThing("http//www.mecssoftware.com\images\carol&dewey.jpg","C\Documents and Settings\owner\desktop\netPic")

            if netDone(netID)=FALSE then go to the frame



            if netDone(netID)= FALSE then go to the frame




            • 3. Re: Download from internet with lingo
              brahim_ayi Level 1

              Hi list,


              Is there a way to download a file using lingo downloadNetThing on an http POST request ?


              I'm using curl with shell xtras but curl ofen bug when the connection is too slow and that's generate an application crash.




              PS: here's a response for this bug on the Haxxe forum :


              << What operating system are you using? Are you possibly using somd kind of

              Windows? If so, that is the reason: we don't have any way to gracefully

              cancel slow function calls in windows, so if the name resolving takes a long

              time there's nothing we can do about it.>>




              • 4. Re: Download from internet with lingo
                Production Monkey Level 3

                The first problem is that I said to not use "exitFrame". Perhaps I should have used stronger language.


                I modified a script from my library to do file downloading and I wrote a behavior to demo how to use it.  Try them out and read the code carefully.




                Paste the following code into a Parent script named "FileDownLoader".



                -- FileDownLoader Parent Script


                property  pActiveNetOperations  --  Data on current network operations in progress.


                -- private static property
                property  this  -- reference to this object. DO NOT access directly.


                ------------------------------------------------------------------------------------------ --------
                -- PUBLIC METHODS
                ------------------------------------------------------------------------------------------ --------


                on getInstance me  -- Use this to get an object instance
                  return me.new()
                end getInstance


                -- NOTE:  if you make changes to this script you MUST call this handler before running your program again,
                --              otherwise your changes will not exist.  ie. in the message window:  script("FileDownLoader").clearStatics()
                on clearStatics me
                  me.script.this = void
                end clearStatics


                -- Method to call to download a file to your computer
                -- Makes callback which returns: Error:Boolean, ErrorMessage:String
                on downLoadFile me, URL, FilePath, CallingOb, CallbackHandler
                  -- store info about this operation for monitoring and callbacks.
                  NetOpData = [#id:0, #CallbackOb:CallingOb, #CallBackHandler:CallbackHandler]
                  NetOpData.id = downloadNetThing(URL, FilePath)
                  -- if there is a callback object and a handler then add this data to pActiveNetOperations. Otherwise we are done.
                  if NetOpData.CallBackOb.ObjectP AND NetOpData.CallBackHandler.SymbolP then
                    pActiveNetOperations.add(NetOpData)   -- store callback info
                    -- setup timeout object to monitor net operation completion
                    if pActiveNetOperations.count = 1 then
                      timeout().new("NetMonitor", 250, #monitorNetOps  , me)  -- check network operations four times a second.
                    end if
                  end if
                end downLoadFile


                ------------------------------------------------------------------------------------------ -------
                -- PRIVATE METHODS
                ------------------------------------------------------------------------------------------ --------


                on new me
                  if me.script.this.objectP then return me.script.this  -- enforce Singleton pattern
                  me.script.this = me
                  pActiveNetOperations = []
                  return me.script.this
                end new


                -- Polls the current network operations to see if they are done or errored out.
                -- returns Error:Boolean, ErrorMessage:String
                on monitorNetOps me, timeOb
                  -- tag net operations that need to be deleted from the queue.
                  ToDelete = []
                  -- Create list of callbacks to be made.
                  CallBacks = [] 
                  -- check each net operation for completion.
                  repeat with Index = 1 to pActiveNetOperations.count
                    NetOpData = pActiveNetOperations[index]
                    --    *** Debug Code.  Uncomment if need more details about the stream status.  ***
                    --    StreamStatus = getStreamStatus(NetOpData.id)
                    --    put StreamStatus
                    --    case StreamStatus.state of
                    --      "Connecting":
                    --        put "Connecting to the server..."
                    --      "Started":
                    --        put  "Download started..."
                    --      "InProgress":
                    --        tempPercent = integer ( 100 * (Float(StreamStatus.bytesSoFar) / StreamStatus.bytesTotal) )         
                    --        put  string (tempPercent) & "% downloaded..."
                    --      "Complete":
                    --        put  "Download completed."        
                    --    end case
                    -- check if net operation is done
                    if netDone(NetOpData.id) then
                      if netError(NetOpData.id) <> "OK" then  -- there was an error
                        ErrorMessage = me.getErrorMessage(netError(NetOpData.id))
                        TempList = [NetOpData.CallBackHandler, NetOpData.CallBackOb, 1, ErrorMessage]  -- handler,object,error, errorMsg
                      else  -- no error. return data passed back
                        ReturnText = netTextResult(NetOpData.id)
                        TempList = [NetOpData.CallBackHandler, NetOpData.CallBackOb, 0, ""]  -- handler,object,error, errorMsg
                      end if
                      CallBacks.add(TempList)  --  store callback info for each net operation that is completed.
                    end if
                  end repeat
                  -- delete completed network operations
                  if ToDelete.count then
                    repeat with Pos = ToDelete.count down to 1
                    end repeat
                  end if
                  -- if no network operations are a happening, then stop monitoring
                  if pActiveNetOperations.count = 0 then
                  end if
                  -- Send data back to calling objects.
                  repeat with CallBack in CallBacks
                    Call(CallBack[1],CallBack[2], CallBack[3], CallBack[4])  -- handler,object,error, errorMsg
                  end repeat
                end monitorNetOps


                -- Returns error message
                on getErrorMessage me, ErrorNum
                  case ErrorNum of
                    0: return("Everything is okay.")
                    4: return("Bad MOA class. The required network or nonnetwork Xtra extensions are improperly installed or not installed at all.")
                    5: return("Bad MOA Interface. See 4.") 
                    6: return("Bad URL or Bad MOA class. The required network or nonnetwork Xtra extensions are improperly installed or not installed at all.")
                    20: return("Internal error. Returned by netError() in the Netscape browser if the browser detected a network or internal error.")
                    4146: return("Connection could not be established with the remote host.")
                    4149: return("Data supplied by the server was in an unexpected format.")
                    4150: return("Unexpected early closing of connection.")
                    4154: return("Operation could not be completed due to timeout.")
                    4155: return("Not enough memory available to complete the transaction.") 
                    4156: return("Protocol reply to request indicates an error in the reply.")
                    4157: return("Transaction failed to be authenticated.")
                    4159: return("Invalid URL.")
                    4164: return("Could not create a socket.")
                    4165: return("Requested object could not be found (URL may be incorrect).")
                    4166: return("Generic proxy failure.")
                    4167: return("Transfer was intentionally interrupted by client.")
                    4242: return("Download stopped by netAbort(url).")
                    4836: return("Download stopped for an unknown reason, possibly a network error, or the download was abandoned.")
                    otherwise: return("Unknown network error.")
                  end case
                end getErrorMessage


                ------------------------------------------------------------------------------------------ --------------------  end of script




                This next script is a Behavior that you attach to a Text member sprite. The default values are set to the ones you posted, but you will get an error because there is something wrong with your URL.  Using a text member sprite provides a way to display a success status or an error message. It also provides a sprite to Click. Note that the call to download a file is initiated on mousedown.



                --  File download Test behavior


                -- Drop on a TEXT MEMBER


                property  pMe  -- this sprite
                property  pURL  -- url to retrieve file from
                property  pFilepath  -- full path to store file on local hard drive


                On GetBehaviorDescription me
                  return "File download Test behavior."
                end GetBehaviorDescription


                On GetPropertyDescriptionList me
                  Props = [:]
                --  Props[#pURL] = [#default:"http://chart.apis.google.com/chart?cht=p3&chd=t%3a90,10&chs=250x100&chl=Pacman", #format:#string, #comment:"URL"]   -- sample
                    Props[#pURL] = [#default:"http://www.mecssoftware.com/images/carol&dewey.jpg", #format:#string, #comment:"URL"]
                  Props[#pFilepath] = [#default:"C:\Documents and Settings\All Users\Desktop\test1.png", #format:#string, #comment:"Full path to store file."]
                  return Props
                end GetPropertyDescriptionList


                on beginSprite me
                  pMe = sprite(me.spriteNum)
                  pMe.member.text = empty  -- clear text member
                end beginSprite


                on mouseDown me
                  -- make call to download a file
                  script("FileDownLoader").getInstance().downLoadFile(pURL, pFilepath, me, #displayStatus)   -- url, full file path, this object, handler to call
                end mouseDown


                on displayStatus me, error, errorMsg  -- callback from the "FileDownLoader" object.
                  if error then
                    pMe.member.text = "There was an error downloading your file." & Return & errorMsg
                    pMe.member.text = "File Downloaded Succesfully."
                  end if
                end displayStatus


                ------------------------------------------------------------------------------------------ --   end of script