I'm new with Apex. I'm downloading images from web with Apex that i'd like to add in Files. Already i'm just adding images to Attachment.

//Download image
Http h = new Http();
HttpRequest req = new HttpRequest();

HttpResponse res = h.send(req);

Blob bodyImg = res.getBodyAsBlob();

//Add image to Attachment
Attachment att = new Attachment(Name = nameImg, Body = 
           bodyImg, ContentType = 'image/png',
insert att;

Is there a way to add document directly in Files or convert/link automatically Attachment to Files ?

  • There are multiple applications to convert Attachments to Files after the fact. Doug Ayers has one; there are others.
    – David Reed
    Apr 19, 2018 at 11:17

1 Answer 1


Use this apex code to create file.

ContentVersion file = new ContentVersion();
file.Title = 'File Name';
file.PathOnClient = 'file_' + Datetime.now().getTime() + '.txt';
file.VersionData = Blob.valueOf('My Content in file_' + 
Datetime.now().getTime() + '.txt');
file.Origin = 'H';
insert file; 

hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.