Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to download a file stored in a database with JSF 2.0
    primarykey
    data
    text
    <p>I need to download a file stored in a database. I think i did correctly the query and called it i just dont know how can i connect that to a button in a JSF page. Also i am wondering, do i have to save that image in a folder at the server before passing it to the JSF page. If so, How can i do that?</p> <p>This is the query i use to return the byte[] from the db:</p> <pre><code>@NamedQuery(name = "downloadGarbage", query = "SELECT g.file FROM Garbage g WHERE g.id :idParam") @Entity public class Garbage implements Serializable { @Lob @Column(nullable = false) private byte[] file; .... </code></pre> <p>Here is a simple EJB that calls that query then get the id:</p> <pre><code>@Stateless(name = "ejbs/FileDownloaderEJB") public class FileDownloaderEJB implements IFileDownloaderEJB { @PersistenceContext private EntityManager em; public byte[] downloadGarbage(Long id) { Query query = em.createNamedQuery("downloadGarbage"); query.setParameter("idParam", id); Object o = query.getSingleResult(); byte[] tmpArray = (byte[]) o; return tmpArray; } </code></pre> <p>Now this is the part that confuses me, how can i that above with the JSF page and managed bean?</p> <pre><code>@ManagedBean @RequestScoped public class DownloadController { @EJB private FileDownloaderEJB fileDownloaderEJB; ... private Garbage garbage; public void startDownload(Long id) { fileDownloaderEJB.downloadGarbage(id); //HOW TO START THE DOWNLOAD? //Other Get Set Methods... } } </code></pre> <p>Also how can i pass that long id to the managedBean from the JSF from within a commandButton? Is this allowed?</p> <pre><code>&lt;!-- How to pass the value of id from --&gt; &lt;h:commandButton action="downloadController.startDownload(#{garbage.id})"&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload