function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bibbo84bibbo84 

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;
			}
			set;
		}
		
		
	public PageReference checkFile() {
		
		
		String fileBody = '';
		
		try{
			fileBody = document.body.toString();
			system.debug(fileBody);
		}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" />

</apex:form>

</apex:page>

 

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


Tanks to all,

 

FP

hemantgarghemantgarg
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.
bibbo84bibbo84

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.

vishal@forcevishal@force
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
IvanWuIvanWu
The code below can solve your problem ,hope it will be useful 
HttpRequest request= new HttpRequest();
request.setBodyAsBlob(csvFileBody);
csvAsString = request.getBody();
Naveen KNNaveen KN
This article may be useful here..
https://www.codengine.in/2019/06/blob-is-not-a-valid-utf-8-string-salesforce-apex.html