Function SRP_HTTP_ASYNC_EXAMPLE(Void) $Insert Msg_Equates $Insert Logical // This value is return when the download is finished. // getDownloadState returns the XMLHTTP .readyState value. // http://msdn.microsoft.com/en-us/library/ms753800(v=vs.85).aspx EQU HTTP_COMPLETED$ To 4 Declare function Msg, TimeGetTime, GetByteSize, OlePutProperty Declare subroutine Msg // This is an optimization to hold the VBS_ASYNC_DOWNLOAD wrapper // script in memory between calls. Common /async_dl_example/ objDLScript@, isInitialized@ ProcessCnt = 0 Done = False$ Loop ProcessCnt += 1 Until Done or ProcessCnt GT 5 On ProcessCnt GoSub InitVars, InitDownloadLibrary, SendDownloadRequest, MonitorDownloadRequest, ProcessDownloadResponse Repeat Return InitVars: // Sample download URLS // Comment out the appropriate line for the type of test download // you want to get. // Text download. JavaScript library from Google CDN * URL = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" ; isBinary = False$ // Binary download. 1MB download file. * URL = "http://mirror.internode.on.net/pub/test/1meg.test" ; isBinary = True$ // Binary download. 10MB download file. * URL = "http://mirror.internode.on.net/pub/test/10meg.test" ; isBinary = True$ * URL = "http://mirror.internode.on.net/pub/test/10meg.test1" ; isBinary = True$ * URL = "http://mirror.internode.on.net/pub/test/10meg.test2" ; isBinary = True$ * URL = "http://mirror.internode.on.net/pub/test/10meg.test3" ; isBinary = True$ // Binary download. 100MB download file. URL = "http://mirror.internode.on.net/pub/test/100meg.test" ; isBinary = True$ // Number of seconds to wait before download times out. NumSeconds = 100 // Convert to milliseconds. TimeOut = NumSeconds * 1000 return InitDownloadLibrary: // Determine if the wrapper script needs to be loaded. If assigned(isInitialized@) else isInitialized@ = '' * isInitialized@ = False$ ;* Uncomment to force loading the wrapper script every time If Not(isInitialized@) then // Wrapper has not been loaded. Load it now. // Create a VBScript control objDLScript@ = OLECreateInstance("MSScriptControl.ScriptControl") objDLScript@->Language = "VBScript" // Read in the insert containing the wrapper code code = XLATE("SYSPROCS", "VBS_ASYNC_DOWNLOAD*" : @APPID<1> ,"","X") // Remove the 'Compile Insert' line that was added by OpenInsight Swap \0D0A\ With @fm In code code = Delete(code, 1, 0,0) Swap @fm With \0D0A\ In code // Add the code to the VBScript control RetVal = objDLScript@->AddCode( code ) isInitialized@ = True$ end return SendDownloadRequest: // Call the wrapper and start the download RetVal = objDLScript@->Run('DownloadAsync', URL , isBinary) return MonitorDownloadRequest: TimedOut = False$ // Init a message box to show activity between attempts to check the // download progress. Def = "" Def = "Downloading..." Def = "GC" Def = NumSeconds * 10 Def = "Time out in " : TimeOut : "ms" MsgUp = Msg(@Window, Def) // Record when the download started to catch if it times out. tc_start = TimeGetTime() ********************************************************************* * Main processing loop to check if the download has finished * ********************************************************************* Loop tc_diff = TimeGetTime() - tc_start // Call the wrapper to check if the download is finished. GoSub GetDownloadState If (tc_diff > TimeOut) then TimedOut = True$ End // Continue processing while the download has not completed and the // download has not timed out. While ((Response NE HTTP_COMPLETED$) And (TimedOut EQ False$)) Do // Main processing loop while downloading GoSub UpdateProgressDisplay GoSub DoProcessingWhileWaiting Repeat If TimedOut then Done = True$ return UpdateProgressDisplay: // Update the status message MsgRet = Msg(@Window, MsgUp, tc_diff / 100, MSGINSTUPDATE$) If MsgRet NE 1 then // User clicked on the Cancel button. Just treat as if the process timed out. TimedOut = True$ end return ********************************************************************* * Main processing loop to check if the download has finished * ********************************************************************* DoProcessingWhileWaiting: // Put code here so OpenInsight can asynchronous process other items while the download continues. return GetDownloadState: Response = objDLScript@->Run('getDownloadState') return ProcessDownloadResponse: // Dismiss the progress message. Msg(@Window, MsgUp) rv = Set_Status(0) // Did processing loop end due to time out? If TimedOut EQ True$ then // Yes, display a status message. RetVal = objDLScript@->run('abortDownload') Msg(@Window, "Download timed out or canceled") end else // No, download completed. // Return the downloaded content as a string or path to a file // containing the binary download data. GoSub GetDownloadContent // Display appropriate return value based on download type If isBinary then LenResponse = Dir(ResponseText)<1> MessageText = "Downloaded binary file to:" MessageText := @TM : @TM MessageText := ResponseText End else LenResponse = Len(ResponseText) MessageText = "Downloaded non-binary content (partial):" MessageText := @TM : @TM MessageText := ResponseText[1, 45] : "..." End If tc_diff EQ 0 then tc_diff = 1 MessageText := @TM : @TM LenResponse = LenResponse / 1000000 tc_diff = tc_diff / 1000 TransferRate = Oconv(Iconv(LenResponse / tc_diff, 'MD2'), 'MD2') MessageText := "Transfer rate: " : TransferRate : " MB/sec" Msg(@Window, MessageText) end return GetDownloadContent: ResponseText = objDLScript@->run('getDownloadContent') return