I am attempting to convert an apex attachment into a blob. My visualforce code is
<div class="mt-5 row">
<div class="d-flex justify-content-center col-lg-12">
<h1>Upload an Attachment To S3</h1>
</div>
</div>
<div class="row">
<div class="d-flex justify-content-center col-lg-12">
<apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
</div>
</div>
<div class="mt-1 row">
<div class="d-flex justify-content-center col-lg-12">
<apex:commandbutton value="Save" action="{!convertDocToBlob}" id="saveButtonForS3" />
</div>
</div>
My apex is
public Pagereference convertDocForS3(){
String sObjID = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment(parentId = sObjID, name=myfile.name, body = myfile.body);
Blob testBlob = convertToBlob(a);
return null;
}
public Blob convertToBlob(Attachment attach){
Blob testBlob = EncodingUtil.base64Decode(attach.body.ToString());
return testBlob;
}
When I attempt to upload at png file, the error I get back is
System.StringException: BLOB is not a valid UTF-8 string
EDIT: The correct thing to do in this case was
Blob testBlob = a.body;
as the attachment was already coming through as a blob.