Skip to content

FILE protocol example

Andrew Lambert edited this page Jan 8, 2023 · 13 revisions

Remarks

The file: protocol is a URI scheme for reading and writing files on the local system. libcURL supports this protocol even though it's not really a network protocol at all.

Examples

Reading a file

Synchronous read

This example performs a synchronous file read on the calling thread.

  Dim curl As New cURLClient
  Dim file As FolderItem  = SpecialFolder.Desktop.Child("test.txt")
  If curl.Get(file.URLPath) Then
    Dim data As String = curl.GetDownloadedData()
  Else
    MsgBox(libcURL.FormatError(curl.LastError))
  End If

Asynchronous read

This example performs an asynchronous file read in a console application, and prints the output directly to the screen. The Get method accepts an optional Writeable object to which downloaded data should be written. Because this is a console application an event loop must be supplied for the asynchronous transfers to run on:

  Dim curl As New cURLClient
  Dim file As FolderItem = SpecialFolder.Desktop.Child("test.txt")
  curl.Get(file.URLPath, stdout) ' stdout implements Writeable
  Do
    App.DoEvents() ' async transfers require an event loop!
  Loop Until curl.IsTransferComplete

  If curl.LastError <> 0 Then 
    Print("curl error: " + Str(curl.LastError))
  End If

Writing a file

Synchronous write

This example performs a synchronous file write on the calling thread.

  Dim curl As New cURLClient
  Dim file As FolderItem = SpecialFolder.Desktop.Child("test.txt")
  If Not curl.Put(file.URLPath, "Hello, world!") Then
    MsgBox(libcURL.FormatError(curl.LastError))
  End If

Asynchronous write

This example performs an asynchronous file write in a console application. Because this is a console application an event loop must be supplied for the asynchronous transfers to run on:

  Dim curl As New cURLClient
  Dim file As FolderItem = SpecialFolder.Desktop.Child("test.txt")
  curl.Put(file.URLPath, "Hello, world!")
  Do
    App.DoEvents() ' async transfers require an event loop!
  Loop Until curl.IsTransferComplete

  If curl.LastError <> 0 Then 
    Print("curl error: " + Str(curl.LastError))
  End If
Clone this wiki locally