Character encoding confusion [SOLVED]

I'd like to have one method for sending file contents from an SDCard via an Ethernet card to a browser.

Particularly, I'd like to use TinyFat's binary read method for both text (.htm) and binary (e.g. .png) files.

When I do a binary read on a HTML file, what gets echo'd to the browser is something like :

60 33 68 79 67 84 89 80 69 32 104 116 109 108 62 10 60 104 ..... which correctly corresponds to the character codes for the html I am trying to send ( and so on....).

What I don't understand is how to inform the browser to correctly interpret the data I'm sending, or how to send it in the correct format... I'm googling myself to death but it's not helping.

The relevant headers that I'm sending (for the .htm) are :

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html; charset=ISO-8859-1");

and the relevant file reading part is :

res=file.openFile(filename, FILEMODE_BINARY);
      if (res==NO_ERROR)
      {
        result=512;
        i=0;
        while (result==512)
        {
          Serial.println("RESULT 512");
          result=file.readBinary();
           for(int i=0;i<result;i++) {
                client.println(file.buffer[i]);
             }
          }
...

Does anybody know what blindingly obvious step I'm missing?!? Many thanks in advance!

client.println(file.buffer[i]);

I presume that this will send a carriage return and a line feed after every character.
Try:

client.print(file.buffer[i]);

Pete

Try

Even better would be to send the whole array at once, not one character at a time. A cast of the file.buffer type (whatever it is) to char * is necessary to get the data sent as letters, instead of bytes.

You really can't have one function that deals with both ASCII and binary files, since the ASCII data is sent using print() or println() and the binary data is sent using write().

Awesome! That was half my problem.

By just changing client.print to client.write I am getting the html correctly output! I'm really annoyed with myself for not spotting that! :astonished:

I've now removed the character by character print with :

client.write(file.buffer,result);

This works beautifully, now I just need to implement a bit of file type handling and hopefully all will work out beautifully! Thanks for the tip!