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
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.
-
Thank you so much this really solve my problem. Can I ask you what is the explanation of this ?– HopeFeb 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. Feb 3, 2016 at 4:49