Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to know whether the following is achievable in Salesforce.

If I pass a string containing URI of a file to an apex class, the file should be uploaded to Salesforce.

share|improve this question
can you expand a bit on this ? – Mohith Shrivastava Jun 11 '14 at 10:42
where is the URI of file located ?On salesforce or other external website ? – Mohith Shrivastava Jun 11 '14 at 10:43
Hi Mohith...As of now we are thinking of using dedicated File Server for this purpose. Eg : \\SERVER_NAME\DIR\FILE.csv etc – ZenSeeker Jun 11 '14 at 10:52
do you mean the file will be on local server and not on internet? – AtulRajguru9 Jun 11 '14 at 11:12
up vote 1 down vote accepted

You can download the content of file by using URL and create attachment out of it.

The only criteria is that you add that url in Remote site setting.

Here is code sample that shows how to do it. Its copied from Here

Http h = new Http();
HttpRequest req = new HttpRequest();
string firstImageURL = '';
//Replace any spaces with %20
firstImageURL = firstImageURL.replace(' ', '%20');
//If you want to get a PDF file the Content Type would be 'application/pdf'
req.setHeader('Content-Type', 'image/jpeg');
HttpResponse res = null;
res = h.send(req);
//These next three lines can show you the actual response for dealing with error situations
string responseValue = '';
responseValue = res.getBody();
system.debug('Response Body for File: ' + responseValue);
//This is the line that does the magic.  We can get the blob of our file.  This     getBodyAsBlob method was added in the Spring 2012 release and version 24 of the API.
blob image = res.getBodyAsBlob();

Attachment n = new Attachment();
//You will want to tie your attachment to some type of custom or standard object
//n.ParentId = myAccount.Id;
n.Name = 'myImage.jpg';
n.Body = image;
//If we were saving a PDF as an attachment the ContentType would be 'pdf'
n.contentType = 'image/jpeg';
insert n;
share|improve this answer
Hi Atul..thanks for such detailed explanation :) – ZenSeeker Jun 11 '14 at 12:32

If you want to upload file from local server follow this example Click Here


public class status{
private final Applicant__c applicant;
public Blob resume {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}

public status(ApexPages.StandardController stdController) {
public PageReference saveApplication() {
   }catch(System.DMLException e){
   return null;

      Attachment attach=new Attachment();
try {
  } catch(System.DMLException e) {
      return null;
  PageReference p = Page.Confirmpage;
        return p;                    


<apex:page standardController="Applicant__c" extensions="status">
<apex:form >
<td>Applicant Name </td>
<td><apex:inputField value="{!Applicant__c.Name__c}"/></td>
<td>CV </td>
<td> <apex:inputFile accept="doc, txt, pdf" filename="{!fileName}" contentType="    {!contentType}" filesize="1000" size="50" value="{!resume}"/> </td>
<td><apex:commandButton id="submitApplicant" value="Submit" action="    {!saveApplication}"/></td>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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