Skip to content

libcURL.EasyHandle.DataAvailable

Andrew Lambert edited this page Aug 9, 2016 · 32 revisions

#libcURL.EasyHandle.DataAvailable

##Event Signature

 Event Function DataAvailable(NewData As MemoryBlock) As Integer

##Parameters

Name Type Comment
NewData MemoryBlock 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; resume the download by calling EasyHandle.Resume.

##Remarks This event handles the CURLOPT_WRITEFUNCTION callback if no DownloadStream is specified. 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
    Return False
  #endif
  Dim curl As New libcURL.EasyHandle
  Dim filestream As BinaryStream = BinaryStream.Create(ToFile)
  Dim fstream As Integer = filestream.Handle(BinaryStream.HandleTypeFilePointer)
  Call curl.SetOption(libcURL.Opts.WRITEDATA, fstream)
  Call curl.SetOption(libcURL.Opts.WRITEFUNCTION, Nil)
  If Not curl.Perform(URL) Then MsgBox(libcURL.FormatError(curl.LastError))
  filestream.Close
  Return True
End Function

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

##See also

Clone this wiki locally