You need to sign in to do that
Don't have an account?
Ajay_SFDC
BLOB is not a valid UTF-8 string Error while reading Excel file
Hi there ,
I have written an Inbound Email service of salesforce . I am sending excel file to this salesforce service . Below is ocde in my method of InboundEmailHandler class :
Messaging.Inboundemail.Binaryattachment[] binaryAttachement = email.binaryAttachments;
if(binaryAttachement !=null && binaryAttachement.size() > 0)
{
for(Messaging.Inboundemail.Binaryattachment bAttach :binaryAttachement )
{
if(bAttach.filename.endsWith('.xls'))
{
String bodyEncoded = EncodingUtil.base64Encode(bAttach.body);
system.debug('&&&&&&&&bodyEncoded'+bodyEncoded);
Blob decoded4Bytes = EncodingUtil.base64Decode(bodyEncoded );
system.debug('&&&&&&&&bodyEncoded'+decoded4Bytes.toString() );
}
}
}
I am getting exception "BLOB is not a valid UTF-8 string" while converting this binary data into string :(In system.debug)
Excel file always gives body in binary format . I want to parse/get contents of the excel file . Is there any way to get the contents of excel file ? Has anyone written parser for excel body ?
Thanks ,
Ajay
I have written an Inbound Email service of salesforce . I am sending excel file to this salesforce service . Below is ocde in my method of InboundEmailHandler class :
Messaging.Inboundemail.Binaryattachment[] binaryAttachement = email.binaryAttachments;
if(binaryAttachement !=null && binaryAttachement.size() > 0)
{
for(Messaging.Inboundemail.Binaryattachment bAttach :binaryAttachement )
{
if(bAttach.filename.endsWith('.xls'))
{
String bodyEncoded = EncodingUtil.base64Encode(bAttach.body);
system.debug('&&&&&&&&bodyEncoded'+bodyEncoded);
Blob decoded4Bytes = EncodingUtil.base64Decode(bodyEncoded );
system.debug('&&&&&&&&bodyEncoded'+decoded4Bytes.toString() );
}
}
}
I am getting exception "BLOB is not a valid UTF-8 string" while converting this binary data into string :(In system.debug)
Excel file always gives body in binary format . I want to parse/get contents of the excel file . Is there any way to get the contents of excel file ? Has anyone written parser for excel body ?
Thanks ,
Ajay
gives out a non String value so it cannot be displayed in the debug log as a string
I dont think its the problem due to system.debug. Even if I remove it I am getting the same error .
Let me know if anyone written the excel parser .
Thanks ,
Ajay
Below line :
Blob decoded4Bytes = EncodingUtil.base64Decode(bodyEncoded );
String strCSVData = decoded4Bytes.toString(); // This line causing error
Thanks
You can try using the HttpRequest class:
This is kind a workaround to get a valid utf-8 string from a value (I haven't found any other way).
This comes from a blog post I wrote some months ago (http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html) when I was playing with blob coming from an HTTP request (you are not making any callout in this 3 lines, but just using the HttpRequest encoding conversion features).
The problem is that it is difficult to then handle the binary part in your file.
Salesforce is not really binary friendly.
Hope this helps
Enrico
also tried the EncodingUtil.base64Encode(csvfile); and didn't work.but this one works great.
Also the explanation in the blog is very clear.
Thanks.
https://www.codengine.in/2019/06/blob-is-not-a-valid-utf-8-string-salesforce-apex.html