Uploading a Document into Salesforce using Visualforce
3:27 PMIn 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.
<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; } }
12 comments
This was exactly what I was looking for. Thanks for posting it! Please also let know if you find how to save it as an attachment to a record.
ReplyDeleteThanks for stopping by and commenting.. Sure, will post the code you asked
ReplyDeletethaks
ReplyDeletePlease tell me how you stored the reference of the file as a Field in the record. I am trying to to the same.
ReplyDeleteCreate a field called DocumentId in your object.. When you insert the document after upload, store this ID in the new field you created.. Later, you can navigate to the document by using the URL .. '/DocumentId'
ReplyDeletewas facing an error"Variable does not exist: folderid".May i know what would be the problem?
ReplyDeleteWeird!!! So you got this error when you try to save the apex class? What is the API version you use? and lastly, can you check the list of fields available for the object DOCUMENT?
ReplyDeletei wanted, how to override Opportunity button in sfdc
ReplyDeleteJust exactly what I needed, thank you very much for sharing this.
ReplyDeletei copy this code and when i am going to save it then it show an error " Variable does not exist: folderID at line 13 column 10 "
ReplyDeleteAblove code si working fine for me but I am facing isssue.
ReplyDelete1- whn i am uploding the file thn the contentType is null for all the uploaded files.
how to get the ContentType of the file..
you can run the given below query.
SELECT Name,AuthorId ,type,Keywords,Url,BodyLength,Body ,FolderId FROM Document
Please tell me How to Dynamically add a watermark to an uploding images using Javascript/Jquery in Visualforce page?
ReplyDelete