Book Image

Primefaces Cookbook Second Edition

Book Image

Primefaces Cookbook Second Edition

Overview of this book

Table of Contents (20 chapters)
PrimeFaces Cookbook Second Edition
Credits
Foreword
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Downloading files


The fileDownload component can be used to stream binary contents, such as files to requesting browsers, by wrapping the components with any JSF command component, such as a button or link.

How to do it…

The value of the fileDownload component should be an instance of org.primefaces.model.StreamedContent. The concrete class org.primefaces.model.DefaultStreamedContent could be used in your implementation, which is also suggested by us.

The following is the backing bean implementation for the file download:

public class FileBean implements Serializable {
  private StreamedContent file;

  public FileBean() {
    InputStream stream = this.getClass().
      getResourceAsStream("/chapter7/PFSamplePDF.pdf");
    file = new DefaultStreamedContent(stream,
      "application/pdf", "PFSample.pdf");
  }

  public StreamedContent getFile() {
    return file;
  }

  public StreamedContent getDownloadFile() {
    return downloadFile;
  }
}

The fileDownload component is wrapped by commandButton...