Reputation: 23
I'm having a problem with apex: I created a dialog that allows me to load a CSV file. However, when I loaded it, it returned the following error message:
09:17:41:021 VARIABLE_ASSIGNMENT [19]|e|"common.apex.runtime.impl.ExecutionException: BLOB is not a valid UTF-8 string"|0x72d1bdb2
In the CSV file, there are symbols like "-" and "_". I suspect this would be the reason why it was failed to load.
This is my code:
public class ImportExcel {
public String fileName {get; set;}
public Blob contentFile {get; set;}
public String[] csvFileLines {get; set;}
public List<String> acclist {get; set;}
public String[] data {get; set;}
public ImportExcel() {
csvFileLines = new String[]{};
acclist = New List<String>();
}
public void readFile() {
try {
fileName = contentFile.toString();
system.debug(fileName);
} catch (Exception e) {
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please upload a valid CSV document');
ApexPages.addMessage(myMsg);
return;
}
csvFileLines=fileName.split('\n');
for (Integer i = 1; i < csvFileLines.size(); i++) {
data = csvFileLines[i].split(',');
acclist.add(csvFileLines[i]);
}
}
}
Upvotes: 0
Views: 1272
Reputation: 375
The issue is with the binary data in your file that your code cannot handle.
The contentFile
is a Blob
and when you use contentFile.toString()
the functions look for compatible UTF-8 strings. Your data might include binary data that toString()
is unable to work with them.
If you need the binary data stored as string anyways you can use EncodingUtil.base64Encode(contentFile)
.
Upvotes: 1