[SalesForce] ‘BLOB is not a valid UTF-8 string’ error

Im reading in a document that the user uploads from the visualFlow:

<apex:inputFile value="{!contentFile}" filename="{!nameFile}" id="file"/>

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 base64Encode the file contents, but the results come out illegible.

String nameFile= EncodingUtil.base64Encode(contentFile);

Best Answer

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).

Related Topic