Search code examples
salesforceapex-codeapex

Convert blob to images and save it to local folder - Apex


I'm able to read blob from the richtext field now need to save it as .jgp/png into my local folder

Matcher imgMatcher = Pattern.compile( '<img(.+?)>' ).matcher(con.Photo__c);
String imageURL = imageTag.substringBetween( ' src="', '"' );
String decodedURL = imageURL.unescapeHtml4();
PageReference page = new PageReference( decodedURL );
Blob bodyBlob = page.getContent();


//something like this
File file=new File('c://myimages');
image img=new image(bodyBlob); 
img.fomrat='jpg';
img.title='myphoto';
file.save(img);

so it stores as image in my local folder. is this possible using apex(salesforce)


Solution

  • This is how i achieved, once saved to salesforce folder from there i downloaded the images, hope it helps someone.

        Matcher imgMatcher = Pattern.compile( '<img(.+?)>' ).matcher(con.Photo__c);
        String imageURL = imageTag.substringBetween( ' src="', '"' );
        String decodedURL = imageURL.unescapeHtml4();
        PageReference page = new PageReference( decodedURL );
        Blob bodyBlob = page.getContent();    
        Document doc=new Document();
        doc.Body=bodyBlob;
        doc.FolderId='salesforce folder Id';
        doc.Name=con.name;
        doc.Type='jpg';
        Insert doc;