2 Replies Latest reply on Jun 14, 2008 3:59 AM by Dan Allen

    I think we need a s:resourceLink tag

    Daniel Hinojosa Master

      I was looking for a way to download a pdf that I have stored on the database which will subsequentially appear in a bean.  I would like the user to download that pdf via a link.  So I went looking through the documentation and the 'Seam in Action' book didn't find what I really needed. 


      So I am suggesting that Seam has a <s:resourceLink/> so that we can download stuff from objects within a context. 


      As an example, this code would download a specification drawing from any product.  <s:resourceLink/> should act like <s:graphicImage/> except that it would make a resource available for download via link.


      <s:resourceLink value="#{product.specification.dataArray}" mimeType="#{product.specification.mimeType}">
         <h:outputText value="Click here to download a pdf of this product"/>
      </s:resourceLink>
      



      Let me know if I should JIRA this..


      Danno

        • 1. Re: I think we need a s:resourceLink tag
          Cloves Almeida Newbie

          I vouche for such tag. The code I use looks like a very nasty hack.



          FacesContext facesContext = FacesContext.getCurrentInstance();
          HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
          response.setContentType("application/octet-stream");
          response.setHeader("Content-disposition", "attachment; filename='myfile.txt'");
          OutputStream out = response.getOutputStream();
          out.write(data);
          out.close();
          facesContext.responseComplete();
          



          IMO, you should also especify an optional filename attribute. If it's set, the component should ignore mimeType and return application/octet-stream. According to HTTP specification, the user agent should present the save as dialog instead of trying th o show the file.

          • 2. Re: I think we need a s:resourceLink tag
            Dan Allen Master

            Take a look at chapter 13 again of Seam in Action (13.2.5 Serving dynamic documents). There is an example of how to do this it a fairly elegant way using Seam's DocumentStore component. Granted, it probably would be nice to implement a component that did all of this work automatically, so I think the idea is good, but you can definitely get some mileage out of what Seam has today.


            Here's a quick copy-paste taking out of context:


            byte[] binaryData = ...;
            DocumentData data = new DocumentData("report"
                new DocumentData.DocumentType("pdf", "application/pdf"),
                binaryData);
            String docId = documentStore.newId();
            documentStore.saveData(docId, data);                         
            String documentUrl =
                documentStore.preferredUrlForContent(                    
                    data.getBasename(),
                    data.getDocumentType().getExtension(),
                    docId);
            redirect(documentUrl);