function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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 ?
Sohit Sharma 4Sohit Sharma 4
I resolved this problem,I am using the javaScript download function in two ways

Visualforce page:
<apex:page controller="VFPageDownloaderController" action="{!downloadAutomatic}">
    <apex:outputText value="{!msg}" escape="false"></apex:outputText>
    
    <script>
  function download(s,filename,ext)
  {
    console.log(s+'Function call');
      const link = document.createElement('a');
  // create a blobURI pointing to our Blob
  link.href = 'data:application/'+ext+';base64,'+s;
  link.download = filename+'.'+ext;
  // some browser needs the anchor to be in the doc
  document.body.append(link);
  link.click();
  link.remove();
  // in case the Blob uses a lot of memory
       setTimeout(() => URL.revokeObjectURL(link.href), 20000);
  }

  </script>
 <apex:outputText value="{!downloadJS}" escape="false"></apex:outputText>
</apex:page>

Visualforce controller:
public class VFPageDownloaderController {
    
    public String msg {get;set;}
    public String downloadJS {get;set;}
    
    //created a function to make the API request
    public void downloadAutomatic(){
        
        String base64String = '';
        
        // download file as a blob from url request
       /* 
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setMethod('GET');
        request.setHeader('Content-Type', 'application/json');
        request.setEndpoint('request url');
       HttpResponse response = http.send(request);
        System.debug(response.getBodyAsBlob());
        Blob b = response.getBodyAsBlob();
          base64String = EncodingUtil.base64Encode(b); */
        
        // download file from ContentVersion
        ContentVersion c = [SELECT Id, VersionData, FileExtension, Title FROM ContentVersion limit 1];
          base64String = EncodingUtil.base64Encode(c.VersionData);
        String fileName = 'abc'; 
        if (base64String == '') {
            msg = 'file Not Found';
        }else {
              downloadJS ='<script> download("'+base64String+'","'+fileName+'","'+c.FileExtension+'"); </script>';
        }     
   } 
}