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);
  • 1
    Can you try ensuring the file is saved in UTF-8 format? For example in Windows Notepad ensure Save As dialog shows Unicode UTF-8. – Andrew Fawcett May 16 '13 at 21:58
  • Yes this works, great. But I use windows command line (cmd) to generate this file on the user side. and I cant seem to find a way to save in this format using cmd. But I guess my question now is far from apex and java. But you just got me one step towards a solution. – user2333346 May 16 '13 at 22:42
  • Great, I've added an answer for future reference and also found an answer regarding command line that might be of some use. – Andrew Fawcett May 17 '13 at 8:05

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

| improve this answer | |
  • 4
    Yes, but the problem is how to automate such a process. – user2333346 May 17 '13 at 14:40
  • I face a similar problem. Users use a downloadable CSV file as a template, key in details and upload from a VF page. Dataloader doesn't work for us, because its not really for end users. But even if the CSV template is saved as UTF-8, when users edit it in Excel and save it, it becomes ANSI again. So yes, the problem is to automate storing CSV in UTF-8 – Vid L Oct 17 '14 at 14:37

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

| improve this answer | |
  • Thank you for the code, i found a solution for this back in May. But thanks for sharing. – user2333346 Jan 29 '14 at 15:37
  • EncodingUtil.convertToHex(input); As soon as you add this it gives the same error as B LOB is not a valid UTF-8 string' error, – Kaushik Ray Sep 28 '14 at 18:26
  • Is there a way to make it preserve line breaks? I have a csv file with weird sings in it I used this method to convert blob to string It works nice but I can't parse lines with my parser as they have disappeared :( – user682217 May 23 '15 at 15:19
  • Does anyone know what the known charset is for a visualforce page that is a pdf? – Tyler Zika Mar 30 '19 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.