What must be done if blob is not a valid utf-8 string?
I am reading in a document that the user uploads from the visualFlow:
And its accessed in apex in this manner:
String nameFile = contentFile.toString();
And it works like a charm. I am able to parse through the document and extract all the information needed, but only for English users. But for Spanish users that's not the case.
Those files have some special characters, and cause a BLOB is not a valid UTF-8 string error.
I've tried to base64 Encode the file contents, but the results come out illegible.
String nameFile= EncodingUtil.base64Encode(contentFile);
If blob is not a valid utf-8 string -
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 xss=removed>
<strong>Note though this code works correctly, but wastes a lot of CPU time</strong>