ESP32_FTPClient Upload image from SD-CARD

33 Views Asked by At

Hello everyone and sorry for the inconvenience. I encountered a problem when I try to copy a jpg image from the SD-CARD to the FTP area. The image is copied correctly but is unreadable. I attach the part of code that performs this operation. I would be grateful if you could help me solve the problem. Best regards.

I expected the image to be uploaded from SD-CARD to FTP. The name and size of the image in the FTP area correspond to the original photo but the content is:

00355/0355-20240212171954.jpg û?#�� #�� 150 Accepted data connection 205 40 38 195 160) em - No anonymous login 220 You will be disconnected after 15 minutes of in�´û?ø @? è� ��ý?`½ý?€ý?¨ý?�� ˆ� ø @? è� ”®ý?�ªý?°ªý?تý?� ˆ� � p_ü? _ü?Ô_ü?� �00355/0355-20240212171954.jpg û?#�� #�� 150 Accepted data connection 205 40 38 195 160) em - No anonymous login

I'm probably wrong with the parameters I supply to the function: ftp.WriteData but I don't know what else to give him.

/************************************************/
/* RICERCA FOTO VIAGGIO DA TRASFERIRE           */
/************************************************/
void CerFotVia(fs::FS &fs, const char * dirname, uint8_t levels)
{
  Serial.printf("Listing directory: %s\n", dirname);

  File root = fs.open(dirname);
  if(!root)
  {
    Serial.println("Failed to open directory");
    return;
  }
  if(!root.isDirectory())
  {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while(file)
  {
    if(file.isDirectory())
    {
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if(levels)
      {
        CerFotVia(fs, file.path(), levels -1);
      }
    }
    else
    {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("  SIZE: ");
      Serial.println(file.size());
      // Trasferimento Immagini del Viaggio
      NumDocCar +=1;
      //TraFotVia(file.name(),file.size());
      TraFotVia(file.path(),file.size());
      // Cancellazione Documento Trasferito
      DelDocVia(SD_MMC,file.path());
    }  
    file = root.openNextFile();
  }
}

/************************************************/
/* CANCELLA DOCUMENTO VIAGGIO TRASFERITO SI FTP */
/************************************************/
void DelDocVia(fs::FS &fs, const char * path)
{
  Serial.printf("Deleting file: %s\n", path);
  if(fs.remove(path))
  {
    Serial.println("File deleted");
  }
  else
  {
    Serial.println("Delete failed");
  }
} 

/************************************************/
/* AVVIA TRASFERIMENTO IMMAGINI                 */
/************************************************/
void TraFotVia(String NomFot,int DimFot)
{
  char Foto[40];
  boolean EsiConFtp = false;
  int NumTenTra = 0;

  // Forma Percorso Cartella Viaggio
  w0 = String(FTP_DIR) + String(DirViaTra) + "/";
  LW = w0.length()+1;
  w0.toCharArray(PatViaFtp, LW);

  Serial.printf("DirViaTra %s , PatViaFtp %s, DimFot %d NomFot , ",DirViaTra,PatViaFtp,DimFot);
  Serial.println(NomFot);

  ESP32_FTPClient ftp (FTP_SER,FTP_POR,FTP_USR,FTP_PWD, 5000, 2);

  // Apertura Immagine
  //w0 = String(DirViaTra) + "/" + NomFot;
  //Serial.println(w0);

  w0 = NomFot.substring(1);
  LW = w0.length()+1;
  w0.toCharArray(Foto, LW);

  while (EsiConFtp == false && NumTenTra < 3)
  {
  ftp.OpenConnection();
  if ( ftp.isConnected() )
    {        
      //ftp.ChangeWorkDir( PatViaFtp );        
      ftp.ChangeWorkDir( FTP_DIR );        
      ftp.InitFile("Type I");
      ftp.NewFile(Foto);
      ftp.WriteData( (unsigned char *)Foto,sizeof(Foto));
      ftp.CloseFile();
      ftp.CloseConnection();
      NumDocTra +=1;
      EsiConFtp = true;         
    }
    else
    {
      delay(500);
      NumTenTra+=1;
    }
  }
}
0

There are 0 best solutions below