This example illustrates the way to upload a file and attach it against any record using Visualforce. The file that you upload using this will be available in the "Notes & Attachments" related list for the record.
Click here if you are looking to upload a file into "Documents".
Step 1: Create an Apex class named "attachmentsample" . Paste the code below.
Step 2:
Create a Visualforce page named "attachment" and paste the code below.
Quick Test:
If you do not wish to do Step3 and 4 do this. Paste the following URL in your browser
http://yoursalesforceinstance.com/apex/attachment?id=0017000000eiDgy
ID should be a Valid Account Id of your instance.
Step 3:
Create a custom button (Detail Page button) on Account and select the new Visualforce page that you created as the source.
Step4:
Add the button created in Step3 to the Account Page layout.
You can test this by navigating to any account and clicking on the button that you created in Step3.
You may test
Click here if you are looking to upload a file into "Documents".
Step 1: Create an Apex class named "attachmentsample" . Paste the code below.
public class attachmentsample {
public attachmentsample(ApexPages.StandardController controller) {
}
Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();
return myfile;
}
Public Pagereference Savedoc()
{
String accid = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
/* insert the attachment */
insert a;
return NULL;
}
}
Step 2:
Create a Visualforce page named "attachment" and paste the code below.
<apex:page standardController="Account" extensions="attachmentsample">
<apex:form >
<apex:sectionHeader title="Upload a Attachment into Salesforce"/>
<apex:pageblock >
<apex:pageblocksection columns="1">
<apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
<apex:commandbutton value="Save" action="{!Savedoc}"/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
Quick Test:
If you do not wish to do Step3 and 4 do this. Paste the following URL in your browser
http://yoursalesforceinstance.com/apex/attachment?id=0017000000eiDgy
ID should be a Valid Account Id of your instance.
Step 3:
Create a custom button (Detail Page button) on Account and select the new Visualforce page that you created as the source.
Step4:
Add the button created in Step3 to the Account Page layout.
You can test this by navigating to any account and clicking on the button that you created in Step3.
You may test