How to convert a JPEG from SPIFFS to Base64?

FILE_PHOTO is in SPIFFS, I need it in base64 in String imageFile.

I'm trying this code but it gives this error:
invalid cast from type 'fs::File' to type 'char*'

File file = SPIFFS.open(FILE_PHOTO, FILE_READ);
    char *input = (char *)file;
    char output[base64_enc_len(3)];
    imageFile = "";
    for (int i=0;i<fb->len;i++) {
      base64_encode(output, (input++), 3);
      if (i%3==0) imageFile += urlencode(String(output));
    }
    file.close();  

How does this snippet of code look like for the conversion to occur successfully ?

I doesn't look very promising. The compiler, as usual, is correct. You can't convert a File object into a char *. How would expect that conversion to work?

What is the correct way ?

Open the file and read its contents?

Anyone else want to help ?

...who don't mind revealing this secret of the universe ?

Have I misunderstood something?
You want to convert the contents of a file to base64, not the File pointer to base64?
Did I miss some fundamental universal verity?

See the title of the post. I have a photo recorded in SPIFFS, only 1 photo. I want to write it to String imageFile in base64 format. The aim is just this.

I tried that but it didn't work either, the ESP32 keeps resetting:
char *input = (char *)file.size();

So, am I correct in imagining that you need to read the contents of a file that is present in the SPIFFS file system?

Correct

Looks to me like you want to read the contents of the file, byte by byte, and present them to the base64_encode function. Consider using the function file.read() for the former.

Examples at SPIFFS in ESP32

Does all of this include the uncompressing of the .jpeg?

I'm using JPEGDecoder.h library

Tried this, imageFile is a Global String.

File jpgFile = SPIFFS.open("/photo.jpg", FILE_READ);
    uint8_t data;
    byte line_len = 0;
    while (jpgFile.available()) {
     data = jpgFile.read();
     line_len++;     
       char *input = (char *)line_len;
       char output[base64_enc_len(3)];
       imageFile = "";
        for (int i=0;i<line_len;i++) {
          base64_encode(output, (input++), 3);
          if (i%3==0) imageFile += urlencode(String(output));
        }
    }
    jpgFile.close();    
    Serial.println(imageFile);

but keeps giving
Rebooting...

I want to read /photo.jpg in base.64 format on Serial Monitor through imageFile

So it stopped the rebooting, but nothing happens

 fs::File jpgFile = SPIFFS.open("/photo.jpg", "r");
    uint8_t data;
    byte line_len = 0;
    while (jpgFile.available()) {
     data = jpgFile.read();
     line_len++;
     if ( line_len >= 320) {
      line_len = 0;
   
       char *input = (char *)line_len;
       char output[base64_enc_len(3)];
       imageFile = "";
        for (int i=0;i<line_len;i++) {
          base64_encode(output, (input++), 3);
          if (i%3==0) imageFile += urlencode(String(output));
        }
    }
    }
    jpgFile.close();    
    Serial.println(imageFile);

That is one of the worst codes I've seen for a long time.

  • while there is data, read it and throw it away
  • after 320 throws, set length to zero, do some crazy pointer stuff
    (which will never be reached, because length only counts to 255, being an uint8_t)
  • then fumble around encoding some pointers, length times, length is zero, so never

really harcore crazy.

Oops, another smart one.

Remember if:
Everyone here copy and paste code, from moderators to beginners. All.

Nobody would copy such crap.

Did you look here?
There's an example function that reads the contents of a file.

I would read the documentation, not some tutorials.

Yes, I saw. And for my bad luck, everything I've been researching, and it wasn't little, gives an example of "Hello World"

This one is what I'm reading right now.

But I do not understand. I'm trying while the person who will help hasn't seen this post yet.

An image, as its size is not known, needs to fill a buffer little by little, like a faucet filling a bucket. When finished, then it converts to base64.

I can no longer start from scratch. Ex: Book: Introduction to C++

Too bad I've been trying to understand things that actually work.

But everyone here copy and paste everything, always. But when pasting some code snippet that can help...