In this article we will be understanding the procedure to upload a file into "Documents" using a Visualforce Page. You can upload a file and save it as an attachment against a record i believe, but i am not sure how to do this. Hence, this topic would only cover uploading a file to "Documents" under a folder you desire.
I actually had to upload a file against a record. I uploaded the file into Documents, and then stored the reference of the file as a Field in the record. This way i can access the document from the record directly at a later point.
Code Sample:
Create the following Visualforce Page
The controller would be named "UploadDoc" and will have the following code
I actually had to upload a file against a record. I uploaded the file into Documents, and then stored the reference of the file as a Field in the record. This way i can access the document from the record directly at a later point.
<apex:inputfile>
is the tag that is used to upload a file.Code Sample:
Create the following Visualforce Page
<apex:page controller="UploadDoc"> <apex:form> <apex:sectionHeader title="Upload a Document into Salesforce"/> <apex:pageblock> <apex:pageblocksection columns="1"> <apex:inputfile value="{!myimage.body}" filename="{!myimage.Name}" /> <apex:commandbutton value="Save" action="{!Savedoc}"/> </apex:pageblocksection> </apex:pageblock> </apex:form> </apex:page>
The controller would be named "UploadDoc" and will have the following code
public class UploadDoc { Public Document mydoc; Public Document getmyimage() { mydoc = new Document(); return mydoc; } Public Pagereference Savedoc() { mydoc.folderid = UserInfo.getUserId(); // Stores in the currennt users "My Personal Documents" folder //mydoc.folderid = 'any other id you want here'; // Fetch Id dynamically by using [Select Id from Folder where Name='My Images']. Specifying a FolderId is mandatory insert mydoc; return NULL; } }