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. Commented May 16, 2013 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. Commented May 16, 2013 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. Commented May 17, 2013 at 8:05

2 Answers 2


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

  • 5
    Yes, but the problem is how to automate such a process. Commented May 17, 2013 at 14:40
  • 1
    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
    – LVS
    Commented Oct 17, 2014 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

  • Thank you for the code, i found a solution for this back in May. But thanks for sharing. Commented Jan 29, 2014 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,
    – SEuser
    Commented Sep 28, 2014 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
    Commented May 23, 2015 at 15:19
  • Does anyone know what the known charset is for a visualforce page that is a pdf?
    – Tyler Zika
    Commented Mar 30, 2019 at 7:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .