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
Andrew DayAndrew Day 

Issue w/Images, Attachments and the API

Hey Guys,

I hope spmepne can help, we have developed a visual force page the allows users to upload and image crop it then we save it as an attachment and this seems to work well.
 
public PageReference SaveImage() 
    {    
        
        System.debug('>>>> Start: SaveImage');
        
		DELETE [SELECT Id FROM Attachment WHERE parentId =:CurrentStudentBio.Id and Name = 'ProfileImage.png'];  
         
        //Attachment
        Attachment attachment = new Attachment();
 		attachment.Body = Encodingutil.base64Decode(imageData.substring(imageData.indexOf(',') + 1));
        attachment.ContentType='image/png';
  		attachment.Name = 'ProfileImage.png';
  		attachment.ParentId = CurrentStudentBio.Id;
  		insert attachment;
        
        System.debug('>>>> End: SaveImage');
        
        return new PageReference('/apex/StudentBio');   
        
}
I am certin this is working correctly as we are able to display this image back to the user on another page using
<apex:image url="/servlet/servlet.FileDownload?file={!BioImage}" style="visibility: {!If(BioImage == null, "hidden", "visible") }"/>
with no issues.

The problem I am now having is when I try and retrive the image using the Rest API i keep getting this error:

{"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. "}

This is what my query looks like
await client.QueryAsync<sforce.Attachment>("select ParentId, Body, ContentType FROM Attachment WHERE ParentId IN ('a2118000000O56a') AND Name = 'ProfileImage.png'");
I am not sure why I am getting this error I can only think the issue is when I save the image but salesforce has no issues displaying it, any help would be appreciated.

Thanks