+ Start a Discussion
rajesh_yrajesh_y 

How to provide file download facility in visual force page

Hi

i wants to provide facility to files which i uploaded

i wants implement this in visualforce page

 

please put code also

thanks in advance

 rajesh

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

How have you uploaded the files?  I.e. are they attachments to records, in the Documents tab, chatter files etc?

 

Assuming its an attachment, you can simply output a link, e.g. if you have access to the attachment in a property named 'attachment', you can do the following:

 

 

<apex:outputLink value="{!URLFOR($Action.Attachment.Download, attachment.id)}" target="_blank">{!attachment.name}</apex:outputLink>

 

 

All Answers

bob_buzzardbob_buzzard

How have you uploaded the files?  I.e. are they attachments to records, in the Documents tab, chatter files etc?

 

Assuming its an attachment, you can simply output a link, e.g. if you have access to the attachment in a property named 'attachment', you can do the following:

 

 

<apex:outputLink value="{!URLFOR($Action.Attachment.Download, attachment.id)}" target="_blank">{!attachment.name}</apex:outputLink>

 

 

This was selected as the best answer
nagalakshminagalakshmi

Hi Bob,

 

I have tried this code. Its working for me. But it is shows the code in another tab of the browser. But i need to down load the attchement. How can i download the attachement.

 

Thanks,

Lakshmi

bob_buzzardbob_buzzard

I suspect that's because the browser knows how to handle that particular type of file.  In that case you have to right click the link and choose 'save as' or similar.

nagalakshminagalakshmi

Hi Bob,

 

Thanks for your reply,

 

Is it not possible to download directly through command button, with right click and save.

 

Thanks,

Lakshmi

bob_buzzardbob_buzzard

The problem here is that its controlled by the browser.  The only way I'm aware of to achieve this is to set the Content-Disposition header on the HTTP response, but you don't have access to that from Apex/Visualforce.

Raj_NRRaj_NR

Hi,

 

I am new to Salesforce. My requirement is when the user clicks on the link the file has to download from Static resource. Can any one help on this?

akschampakschamp

You can do that using below code

 

 

<a href="{!URLFOR($Resource.Templatefile)}" target="_blank"> Click here </a>

 

 

where Templatefile is your static resource name.


Raj_NR wrote:

Hi,

 

I am new to Salesforce. My requirement is when the user clicks on the link the file has to download from Static resource. Can any one help on this?


 

Raj_NRRaj_NR

Thanks Champ. Now it is working fine...

Dr WhoDr Who
You do have access to the Content-Disposition in APEX.

Do something like the following in your VFP controller constructor

ApexPages.currentPage().getHeaders().put('Content-Disposition', 'attachment; filename="Setup.exe"');
 
Tapasvini MakvanaTapasvini Makvana
if it is chatter file than how it can be achieved ?