Skip to content

libcURL.EasyHandle.DataAvailable

Andrew Lambert edited this page Jun 17, 2015 · 32 revisions

#libcURL.EasyHandle.DataAvailable

##Event Signature

 Event Function DataAvailable(NewData As String) As Integer

##Parameters

Name Type Comment
NewData String The data that is available.

##Return value The number of bytes successfully consumed which should be equal to the number of bytes provided. You may also return CURL_WRITEFUNC_PAUSE to consume no data and pause the download.

##Notes This event handles the CURLOPT_WRITEFUNCTION callback. It is raised by EasyHandle.WriteCallback. The new data is discarded once this event returns.

You must return the number of bytes that were successfully handled; if the number of bytes handled is different from the number of bytes provided then libcURL will assume that an error has occurred.

##A neat trick

On Linux and Mac OS X, you may optionally replace this callback with a file pointer which libcURL will automatically write downloaded data to. For example:

Function DownloadToFile(URL As String, ToFile As FolderItem) As Boolean
  #If TargetWin32 Then
    Raise False
  #endif
  
  Dim curl As New libcURL.EasyHandle
  Dim filestream As BinaryStream = BinaryStream.Create(ToFile)
  Dim fstream As Integer = filestream.Handle(BinaryStream.HandleTypeFilePointer)
  If Not c.SetOption(libcURL.Opts.WRITEDATA, fstream) Then Break
  If Not c.SetOption(libcURL.Opts.WRITEFUNCTION, Nil) Then Break
  If Not c.Perform(URL) Then Break
  out.Close
  Return True
End Function

Attempting to do this under Win32 will cause libcURL to crash hard.

##See also

Clone this wiki locally