1

I have a visualforce page in which there is a button called download.
If I click that button then it will download all the account records as text file. For this use case how can I implement.
Do not redirect to another visualforce page
Can someone suggest some ideas to implement?

My vfp code:

<apex:page standardController="Account" extensions="AccountDownload" recordSetVar="accounts" sidebar="false">    
    <apex:form >    
        <apex:pageBlock title="Archiver For Account">   
        <apex:pageBlockSection >                
            <apex:commandButton action="{!Download}" value="download" id="theButton" /><br/>    
             <apex:outputPanel id="panel1" rendered="{!ren}">   
                 <apex:pageBlockTable value="{!accounts}" var="ac">    
                     <apex:column value="{!ac.Name}"/>   
                     <apex:column value="{!ac.AccountNumber}"/>  
                     <apex:column value="{!ac.AnnualRevenue}"/>   
                     <apex:column value="{!ac.Phone}"/>   
                     <apex:column value="{!ac.Type}"/>       
                 </apex:pageBlockTable>                  
             </apex:outputPanel>   
        </apex:pageBlockSection>   
        </apex:pageBlock>           
    </apex:form>     
</apex:page>    

Controller:

   public class AccountDownload {    
    public boolean ren{get;set;}
    public AccountDownload(ApexPages.StandardSetController controller) {
        ren=false;
    }
    public PageReference Download(){
        ren=true;
        return null;

    }
}

3 Answers 3

2

You would need to create another VF page with contentType="text/csv#filename.csv".

On clicking the download button, from the method you can return a page reference to this new page.

<apex:page controller="AccountDownload" contentType="text/csv#filename.csv" >

<apex:repeat value="{!accounts}" var="acc">

{!acc.Name},{!acc.AccountNumber},{!acc.AnnualRevenue},{!acc.Phone}, {!acc.Type},

</apex:repeat>
</apex:page>
1

You may use the renderAs property of page to set it as "PDF" sothat your page will be rendered as a PDF. another way is to define the contenttype of your page as MS word. so after clicking on "Download" button, the page content will be downloaded as word file.

3
  • I want to download particular section only Commented Apr 2, 2015 at 8:39
  • I want to download that outpanel content only Commented Apr 2, 2015 at 9:30
  • 1
    Visualforce does not allow you to download a particular part of page... you probably you will need to create a new page with particular section, set contenttype of new page as word, and navigate to new page on click of download button.
    – Anup
    Commented Apr 3, 2015 at 17:51
0

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>';
        }     
   } 
}
1
  • 1
    Welcome to SFSE and thank you for your contribution. Instead of posting same answer on similar questions, kindly add a description and a link to the first answer you have posted so as to avoid duplicating answers. Because if an edit is required later in the answer, this means multiple answers must be edited
    – Shamina
    Commented May 28, 2022 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .