'BLOB is not a valid UTF-8 string' error?

While it does not appear to be clearly documented, having scanned a number of forum posts about this. The Blob type only supports UTF-8 encoded strings, you must ensure that the file you're uploading complies with this encoding, otherwise you will get this error in cases where you have special characters.

To ensure your file is UTF-8, for example in Windows Notepad make sure that you select to save as Unicode UTF-8 in the Save As dialog, other applications will likely have a similar option (for command line this answer might be of use).


Try this code to convert Blob in known charset to UTF-8 string

    /**
    @param input    Blob data representing correct string in @inCharset encoding
    @param inCharset    encoding of the Blob data (for example 'ISO 8859-2')
*/
public static String blobToString(Blob input, String inCharset){
    String hex = EncodingUtil.convertToHex(input);
    System.assertEquals(0, hex.length() & 1);
    final Integer bytesCount = hex.length() >> 1;
    String[] bytes = new String[bytesCount];
    for(Integer i = 0; i < bytesCount; ++i)
        bytes[i] =  hex.mid(i << 1, 2);
    return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
}

Note though this code works correctly, but wastes a lot of CPU time