readToClient() this function reads data from the file and sends it into the ‘Client’ connection. It is a helper-function that facilitates file uploads or tcp -based data transfer. The function is non-blocking but returns after the next file-data-chunk was transmitted, the end of the files was reached or the connection was closed.


readToClient( opt_Client ); – reads data from the file in (512 byte) chunks and sends it to the ‘Client’ connection.


Client* opt_Client – pointer to an object of the Arduino core class ‘Client’ or a derived class e.g. CANIP_Client, WiFiClient, EthernetClient etc. For http requests (file upload / downloads) there are more convenient HTTP_Client classes. A client needs to have an established connection to a tcp-peer before invoking this function and the file needs to be opened first.


-2: error while sending data to the tcp-client connection (e.g. Client timeout)
-1 : client connection lost / closed before the end of the file could be reached.
0: finished. all data from the file was send.
>0: number of bytes that could successfully be read from the file and send to the client connection.

Example Code

This example uploads a file from SerialFlash to a server using a POST request via a CANIP connection to the internet.

#include "SerialFlash.h"
#include "CANIP.h"
void setup()
  int  i_ret; 
  String s_server = "";
  String s_path = "/upload.php";
  String s_fileName = "myFile.txt";
  CANIP_Client t_postClient(&MCAN0, &BOOTLOADER_SETTING);    
  // Check if the file exists in the Serial Flash
  SerialFlashFile tempFile =;
    Serial.println("File:" + s_fileName + " does not exist.");
    while(1){ yield(); };
  Serial.println("Connected\nPosting file");
  if(!t_postClient.httpPOSTStart(s_server, s_path, String("myPHPscript"),
      tempFile.size(), s_fileName))
    Serial.println("Could not connect to the server");
    while(1){ yield(); };
  do {
    i_ret = tempFile.readToClient(&t_postClient);
  }while(i_ret > 0);
  if(i_ret != 0)
    Serial.println("Error uploading the file:"+String(i_ret));
    Serial.println("\nUpload OK");
  i_ret = t_postClient.httpPOSTEnd();
  Serial.println("Server response: "+String(i_ret));
  if(i_ret >= 0) // Read the response of the server
    while(t_postClient.connected() || (t_postClient.peek() != -1))
      if(t_postClient.peek() != -1)
  Serial.println("\n -- connection closed --");
void loop()
{ }

see example ‘File -> Examples -> SerialFlash -> examples -> RecordsetFiles’
Please note that appropriate php-receiver code needs to be available on the server (see code suggestion also in the example project)

Notes and Warnings

This is a non blocking function so that an application that should transfer data to a server can do so without stalling the main task.

Note that in order to use this function, the ‘File’ has to be opened first and the ‘Client’ has to have an established connection to its peers.

See also

GitHub – PaulStoffregen/SerialFlash – org. code this library is based on
c_str() – function to create a 0-terminated char array from a String
open() – open a file for read/write operations
writeFromClient() – receive file data from the tcp-connection (webserver)