1

I have added a wrapper to display my files on a visualforce page. The wrapper includes a checkbox. I want to download all checked files at once.

The working html tag is as follows:

<apex:outputLink styleClass="btn" target="_blank" 
                style="text-decoration:none;padding:1px;"
                value="/sfc/servlet.shepherd/version/download/{!fileIdsToDownload}?" >
                    Download Checked Files
         </apex:outputLink>

And my code to update the fileIdsToDownload is below:

  //Set Variable used to capture all file id's to download
  public void DownloadCheckedFiles(){
       //string for file ids to download separated by /
     String fileIdsString = null;     
     
     for(fileWrapper fw : wrapFileList){
       if(fw.pickme == True){
          if (fileIdsString == null){
            fileIdsString = fw.converWrap.id;
          } 
          else{
           fileIdsString = fileIdsString + '/' + fw.converWrap.id; 
          }
       }
     }
      
    fileIdsToDownload = fileIdsString;
      //clear out temp variable when done
    fileIdsString = null;
    system.debug(LoggingLevel.Info, '3 here is update list ' + fileIdsToDownload);

  }

I have an action region on the text boxes that runs the DownloadCheckedFiles method. But what I would like to do is to use a button that loops through the checkboxes and triggers the download. The way I have it now, the page refreshes after each checkbox is checked or unchecked. I was unable to figure a way to handle the download in my apex method. Any help would be greatly appreciated! Fred

1 Answer 1

1

The VF controller code should look like this (I renamed your variables to be more concise and follow conventions). Note use of getter to return the concatenated FileIds

public String downloadableFileIdsAsString {
  get{  // use getter to return concatenated fileIds as serveltShepherd wants
    Id[] downloadableFileIds = new List<Id>();
    for (FileWrapper fw: this.fileWrappers) {
        downloadableFileIds.addAll(
          fw.pickMe 
            ? new List<Id>{fw.converWrap.id}
            : new List<Id>());
    }
    return String.join(downloadableFileIds,'/');
  } private set;

 // Action Method - downloadCheckedFiles
 public void downloadCheckedFiles() {}

and your VF page should have this:

<apex:outputLink styleClass="btn" target="_blank" 
      style="text-decoration:none;padding:1px;"
      value="/sfc/servlet.shepherd/version/download/{!downloadableFileIdsAsString}?" >
                Download Checked Files
</apex:outputLink>
11
  • Thank you. Is there a better way to trigger the download as part of the button rather than using the outputlink? I would prefer not to have the page rerender after each checkbox is clicked or unclicked.
    – Frank
    Commented Mar 19, 2021 at 12:45
  • Use actionRegion to limit scope of rerender. Command buttons can initiate actions
    – cropredy
    Commented Mar 19, 2021 at 14:05
  • Could I expand my setter on my wrapper object so that when someone checks a box on one of the records, it triggers updating my variable with all the id's?
    – Frank
    Commented Mar 19, 2021 at 14:41
  • Why? The wrapper maintains state, the getter fetches the selected items but only when needed
    – cropredy
    Commented Mar 19, 2021 at 14:43
  • 1
    @FelixvanHove - I don't think so; I figured this out by observing the URL when requesting multiple files as zip
    – cropredy
    Commented Nov 7, 2023 at 19:43

You must log in to answer this question.

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