Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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 at 10:42
    
where is the URI of file located ?On salesforce or other external website ? –  Mohith Shrivastava Jun 11 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 –  RisingPhoenix Jun 11 at 10:52
1  
do you mean the file will be on local server and not on internet? –  AtulRajguru9 Jun 11 at 11:12

2 Answers 2

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 = 'http://www.myDomain.com/myImage.jpg';
//Replace any spaces with %20
firstImageURL = firstImageURL.replace(' ', '%20');
req.setEndpoint(firstImageURL);
req.setMethod('GET');
//If you want to get a PDF file the Content Type would be 'application/pdf'
req.setHeader('Content-Type', 'image/jpeg');
req.setCompressed(true);
req.setTimeout(60000);             
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 :) –  RisingPhoenix Jun 11 at 12:32

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

/***Controler***/

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) {
this.applicant=(Applicant__c)stdController.getRecord();
}
public PageReference saveApplication() {
  try{
    insert(applicant);                                       
   }catch(System.DMLException e){
  ApexPages.addMessages(e);
   return null;
}

if(resume!=null){
      Attachment attach=new Attachment();
      attach.Body=resume;
      attach.Name=filename;
  attach.ContentType=contentType;
  attach.ParentID=applicant.id;
try {
      insert(attach);
  } catch(System.DMLException e) {
      ApexPages.addMessages(e);
      return null;
  }
  }
  PageReference p = Page.Confirmpage;
        p.setRedirect(true);
        return p;                    
  }
} 

/***PAGE***/

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

Your Answer

 
discard

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.