wwHTTP::OnHttpBufferUpdate

The OnHttpBufferUpdate() method is called whenever a buffer is downloaded from the server and allows you to display download status information for large file downloads. It's also useful for freeing up the UI to avoid blocking HTTP calls that freeze the FoxPro UI when requests take too long.

You can either subclass the wwHttp class and override the OnHttpBufferUpdate() method, or you can use BINDEVENT to bind the method:

loHttp = CREATEOBJECT("wwHttp")
BINDEVENT(loHttp,"OnHttpBufferUpdate",THISFORM,"OnMyHttpBufferUpdateHandler")

The implementation of the OnMyHttpBufferUpdateHandler() then uses the signature described in the example below.

o.onhttpbufferupdate(lnBytesDownloaded,lnBufferReads,lcCurrentChunk, loHttp)

Parameters

lnBytesDownloaded
Number of bytes that have been downloaded so far.

lnBufferReads
Number of reads that have occurred. The following special values can be passed in here: 0 - HTTP Header for the request -1 - Request was completed or cancelled. See lHttpCancelDownload.

lcCurrentChunk
The current block of bytes read from the Request.

**loHttp **
wwHttp instance for this buffer update. Useful if you're using BINDEVENT to route the event to another object rather than subclassing. For subclasses you can also use the THIS. pointer instead to reference the FTP object.

Remarks

Be careful with what kind of code you put into the 'event' method since it fires on every buffer update. Keep code in this method and any called methods to a minimum to avoid download slowdowns.

If you want things like Cancel buttons you will need to use DOEVENTS in the event code to allow the click events to fire. even so the responsiveness of the button may not be very good due to VFP's limited concurrent processing capabilities.

We also suggest that you increase your buffer size so that this event doesn't fire so frequently. Increasing nHTTPWorkBufferSize on the Http instance can significantly improve the performance of HTTP operations.

Example

Using Subclassing
DO wwHTTP
loHttp = CREATEOBJECT("wwHttp_WithEvents")

*** Make the HTTP call - lcHTML is set but output is not used
*** the output written by this example is written from the
*** event handler as data is retrieved
lcHTML = loHttp.HttpGet("http://www.microsoft.com")

* create sub-class of wwHttp and implement OnHttpBufferUpdate
DEFINE CLASS wwHttp_WithEvents as wwHttp

FUNCTION OnHttpBufferUpdate
LPARAMETERS lnbytes,lnbufferreads,lccurrentchunk, loHttp

DO CASE
    *** 0 - Http Header
	CASE lnBufferReads = 0
		lcHttpHeader = lcCurrentChunk
    *** -1 - done or cancelled or failed
	CASE lnBufferReads = -1
		* done or cancelled 		
		IF THIS.lhttpcanceldownload 
			? "Cancelled"
		ELSE
			? "---"
			? "Completed..."
		ENDIF
	*** anything else - data has been read
	CASE lnBufferReads > 0
	    WAIT WINDOW TRANSFORM(lnBytes,"9,999,999") + " of " + TRANSFORM(THIS.nContentSize,"9,999,999") + " bytes" NOWAIT 
ENDCASE

*** You can also accumuluate the data incrementally
*** to allow users to look at data as it comes in
? lcCurrentChunk

ENDDEFINE
Using BINDEVENT
DO wwHTTP
loHttp = CREATEOBJECT("wwHttp")

*** Object that will handle the event. This could be a form 
*** or any other object instance on which you implement
*** the OnHttpBufferUpdate event handler code
loSink = CREATEOBJECT("HttpEventSink")

*** Bind event from wwHttp to the event handling object
BINDEVENT(loHTTP,"OnHttpBufferUpdate",loSink,"OnHttpBufferUpdate")

*** Make the HTTP call - lcHTML is set but output is not used
*** the output written by this example is written from the
*** event handler as data is retrieved
lcHTML = loHttp.HttpGet("http://www.microsoft.com")

RETURN

*** If you have a class instance - a Form for example
*** you can use that class as the Event sink and simply
*** add the method below to it
DEFINE CLASS HttpEventSink as Custom

FUNCTION OnHttpBufferUpdate
LPARAMETERS lnbytes,lnbufferreads,lccurrentchunk, loHttp

DO CASE
    *** 0 - Http Header
	CASE lnBufferReads = 0
		lcHttpHeader = lcCurrentChunk
    *** -1 - done or cancelled or failed
	CASE lnBufferReads = -1
		* done or cancelled 		
		IF loHttp.lhttpcanceldownload 
			? "Cancelled"
		ELSE
			? "---"
			? "Completed..."
		ENDIF
	*** anything else - data has been read
	CASE lnBufferReads > 0
	    WAIT WINDOW TRANSFORM(lnBytes,"9,999,999") + " of " + TRANSFORM(loHTTP.nContentSize,"9,999,999") + " bytes" NOWAIT 
ENDCASE

*** Dump data incrementally
? lcCurrentChunk

ENDDEFINE

See also:

West Wind Web Connection | wwHTTP::lHttpCancelDownload | wwHTTP::HTTPCanceldownload

© West Wind Technologies, 2023 • Updated: 06/29/19
Comment or report problem with topic