Sample code for 30+ languages & platforms
PureBasic

Bandwidth Throttled Asynchronous HTTP Upload

See more Upload Examples

Demonstrates how to do an HTTP upload asynchronously in a background thread with limiting the rate to an approximate number of bytes/second. The only difference between this example and one without bandwidth throttling is that the BandwidthThrottleUp property is set.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkUpload.pb"

Procedure ChilkatExample()

    success.i = 0

    upload.i = CkUpload::ckCreate()
    If upload.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Specify the page (ASP, ASP.NET, Perl, Python, Ruby, CGI, etc)
    ; that will process the HTTP Upload.
    CkUpload::setCkHostname(upload, "www.mywebserver.com")
    CkUpload::setCkPath(upload, "/receiveUpload.aspx")

    ; Add one or more files to be uploaded.
    CkUpload::ckAddFileReference(upload,"file1","dude.gif")
    CkUpload::ckAddFileReference(upload,"file2","pigs.xml")
    CkUpload::ckAddFileReference(upload,"file3","sample.doc")

    ; Set the BandwidthThrottleUp property to throttle to approx 64K/second
    CkUpload::setCkBandwidthThrottleUp(upload, 65536)

    ; Begin the HTTP upload in a background thread:
    success = CkUpload::ckBeginUpload(upload)
    If success <> 1
        Debug CkUpload::ckLastErrorText(upload)
    Else
        Debug "Upload started..."
    EndIf

    ; Wait for the upload to finish.
    ; Print the progress as we wait...
    While (CkUpload::ckUploadInProgress(upload) = 1)
        ; We can abort the upload at any point by calling:
        ; upload.AbortUpload();

        ; Display the percentage complete and the number of bytes uploaded so far..
        ; The total upload size will become set after the upload begins:
        Debug Str(CkUpload::ckPercentUploaded(upload)) + "% " + Str(CkUpload::ckNumBytesSent(upload)) + "/" + Str(CkUpload::ckTotalUploadSize(upload))

        ; Sleep 2/10ths of a second.
        CkUpload::ckSleepMs(upload,200)

    Wend

    ; Did the upload succeed?
    If CkUpload::ckUploadSuccess(upload) = 1
        Debug "Files uploaded!"
    Else
        Debug CkUpload::ckLastErrorText(upload)
    EndIf



    CkUpload::ckDispose(upload)


    ProcedureReturn
EndProcedure