+ Start a Discussion

BLOB is not a valid UTF-8 string and € symbol

Hi to all.


I'm trying to build a loader for csv file. As you can see from the code I load my csv file in an attachment. After I take the body of the attachment and insert it into a string using the toString (). I noticed that when in the csv file there is the € symbol I get an exception of type 'BLOB is not a valid UTF-8 string'. Do you have any suggestion?


My Controller

public with sharing class DocumentwithEuroController {

	public Attachment document {
			get {
				if (document == null)
					document = new Attachment();
				return document;
	public PageReference checkFile() {
		String fileBody = '';
			fileBody = document.body.toString();
		}catch(exception e){
			if(e.getmessage() == 'BLOB is not a valid UTF-8 string'){
				ApexPages.addMessage(new ApexPages.message(ApexPages.severity.WARNING, 'In the csv file there are stressed symbols. The upload is blocked'));	
		return null;




My page.


<apex:page controller="DocumentwithEuroController" >
<apex:form >

<apex:pageMessages id="pms1" />

<label for="ifl"
						style="text-align: left; font-weight: bold; padding-right: 0.69em;">Selezionare
					il file tracciato(.csv):</label>
					<apex:inputFile id="ifl" value="{!document.body}"
						filename="{!document.name}" styleclass="btn" />
						<apex:commandButton value="Load csv" action="{!checkFile}"
				status="loadCsv" />




For me is very important, i must resolve this proble by tomorrow 

Tanks to all,



Salesforce supports UTF-8 encoded files for processing, so you just need to re-save your csv file, first open your csv file in notepad, click on save as, and change its encoding to UTF-8, then try this file tn upload.

Is there a way to automatically convert in utf-8 my csv file on loading? I can't ask my customer to convert the file.

Maybe, urlEncode method would help you here. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_encodingUtil.htm This method here: urlEncode
The code below can solve your problem ,hope it will be useful 
HttpRequest request= new HttpRequest();
csvAsString = request.getBody();
Naveen KNNaveen KN
This article may be useful here..