+ Start a Discussion
Simon234Simon234 

Choose an image from the directory and Upload it as a File

Now it loads an Attachment like a File. Can I choose a file from my computer? How can I do that?
public void sss(){        
        Attachment at = [Select Body, Id, Name, OwnerId,ParentId From Attachment LIMIT 1];
 
		ContentVersion conVer = new ContentVersion();
		conVer.ContentLocation = 'S';
		conVer.PathOnClient = at.Name;
		conVer.Title = 'tttt';
		conVer.VersionData = at.Body;
		insert conVer;
        
        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion
                     WHERE Id =:conVer.Id].ContentDocumentId;
                
        ContentDocumentLink cl = new ContentDocumentLink(
            LinkedEntityId = 'a071r00001Hnn2uAAB',
            ContentDocumentId = conDoc, ShareType = 'I');
		insert cl;
    }
VF Page:
<apex:page standardController="Cand__c" extensions="uplClass">
    <apex:form >
      <apex:pageBlock title="Upload Attachment" >
          <apex:inputFile style="width: 100%" value="{!at.Body}" fileName="{!at.Name}" accept="image/*" />
          <apex:commandButton value="But" action="{!sss}" />     
      </apex:pageBlock>
  </apex:form>
</apex:page>