Function SRP_HTTP_ASYNC_EXAMPLE2(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 SRP_COM, Msg, TimeGetTime, GetByteSize, OlePutProperty Declare subroutine SRP_COM, Msg, GetTempFileName, GetTempPath // 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: return SendDownloadRequest: // Start up the API and make the download request objXmlHttp = '' If SRP_COM(objXmlHttp, "CREATE", "Msxml2.XMLHTTP.3.0") then SRP_COM(objXMLHTTP, "CALL", "open", "GET", url, True$) GoSub ProcessError If isBinary then SRP_COM(objXMLHTTP, "SET", "responseType", "blob") end SRP_COM(objXMLHTTP, "CALL", "send") GoSub ProcessError end else GoSub ProcessError end 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: // Assume function failed as default return getDownloadState = "err" getDownloadState = SRP_COM(objXMLHTTP, "GET", "readyState") // getDownloadState will either be 'err' or the .readyState value Response = 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. SRP_COM(objXMLHTTP, "CALL", "abort") 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 = '' If isBinary then // Since it is binary, get the responseBody Response = SRP_COM(objXMLHTTP, "GET", "responseBody") GoSub ProcessError // Write the response to disk TempPath = Str(\00\, 1024) GetTempPath(Len(TempPath), TempPath) Convert \00\ to '' in TempPath ResponseText = Str(\00\, 1024) GetTempFileName(TempPath, \00\, 0, ResponseText) OSWrite Response to ResponseText end else // Return the downloaded content as a string ResponseText = SRP_COM(objXMLHTTP, "GET", "ResponseText") GoSub ProcessError end return ProcessError: // Check for COM error If SRP_COM("", "HASERROR") then Msg(@Window, SRP_COM("", "ERROR"):@FM:@FM:@FM:"H") end return