3

I have created a functionality wherein I can upload a csv to salesforce. The problem I have at the moment is that when I try to upload an ANSI encoding, an error will occur 'Blob is not a valid UTF-8 string'. How can I upload my file using apex without modifying the current file encoding ? I've tried to use some answers in the internet but it didn't solve my issue. Please help...

1 Answer 1

3

Try this code. It will help you.

    oppList = new List<opportunity>();
    HttpRequest tmp = new HttpRequest();
    tmp.setBodyAsBlob(csvFileBody.body);
    csvAsString = tmp.getBody();
    csvFileLines = csvAsString.split('\n');


    for(Integer i=1;i<csvFileLines.size();i++){
        string[] csvRecordData = csvFileLines[i].split(',');

   }

It will solve your problem.

2
  • Thank you so much this really solve my problem. Can I ask you what is the explanation of this ?
    – Hope
    Commented Feb 3, 2016 at 1:00
  • @Hope In this way HttpRequest convert the encoding. Actually I also face same issue and use this solution. That's all info i have. Glad this helps. Commented Feb 3, 2016 at 4:49

You must log in to answer this question.

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