Tuesday 31 March 2015

Uploading a Document in Salesforce Using Visualforce Page


Some times we need to upload files such as image files,word document etc, and also need to use its reference to use it in our VF page .

In that case we have to follow these steps

In that case we have to follow these steps

Step 1: Click on the document Tab

Step 2: Select the file from your local system to upload & also choose the folder in which you want to save it

Step 3: Keep its reference as a record to use it, if you want to access it directly form record

Instead of following these above steps, we can do these in only one visual-force page.

In this tip we will be understanding the procedure to upload a file into "Documents" using a Visual-force Page.

Hence, this topic would only cover uploading a file to "Documents" under a folder you desire.

<apex:inputfile>
 is the tag that is used to upload a file.



Sample Code

 FileUpload : Sample Visual-force page


 <apex:page controller="FileUploadController" showHeader="true" standardStylesheets="false">

<head>

<script type="text/javascript">


function SelectFile( fileUrl )

{

// window.opener.SetUrl( url, width, height, alt);

parent.SetUrl( fileUrl ) ;

}

</script>

</head>

<apex:messages />

<apex:form >

<apex:inputFile value="{!document.body}" filename="{!document.name}"/>

<br />

<apex:commandButton value="save" action="{!doUpload}" />

</apex:form>

<apex:pageBlock rendered="{!hasImage}" >

<img src="{!url}" width="200" height="160" />

<script>

this.onload = function() {

SelectFile('{!url}');

}

</script>

</apex:pageBlock>

</apex:page>




FileUploadController: Apex Class


 public class FileUploadController{

public Document document {get; set;}

public String url {get; set;}

public boolean hasImage {get; set;}



public FileUpload() {

hasImage = false;

document = new document();

}


 public PageReference doUpload() {

if (document.body != null) {

System.debug('@@@@@@@@ document:' + document.name);

 if (document.name != null) {

System.debug('@@@@@@@@ doSave');

 Document d = document;

System.debug(document);

System.debug('@@@@@@@@ document name: '+d.name);

System.debug(d);



d.folderid = UserInfo.getUserId(); //store in Personal Documents


 /*
d.folderid
 = 'any other id you want here'; // Fetch Id dynamically by using [Select Id from Folder where Name='My Images'].*/

System.debug('@@@@@@@@ document folder id: '+d.folderid);

d.IsPublic = true;

 try {

insert d;

url = DocumentUtil.getInstance().getURL(d);

hasImage = true;

} catch (Exception e) {

ApexPages.addMessages(e);

url = '';

}

 d.body = null;

 System.debug('@@@@@@@@ document object id: '+d.id);

String url = DocumentUtil.getInstance(d).getURL();

System.debug('######## document content type: '+d.type);

}

 }

 PageReference page = new PageReference('/apex/FileUpload');

//page.setRedirect(true);

return page;

}

 static testmethod void testFileUpload() {

FileUpload fu = new FileUpload();

fu.doUpload();

 fu.document.Name = 'test1';

fu.document.Body = Blob.valueOf('test1');

 fu.doUpload();

 }

 }

7 comments: