Question

I have been trying to find out a way to set content type for static resources within my web application, which are provided as downloadable links. For example I have a .CSV file and when I provide this link on the browser, upon click it's opening in a new window. What I really want is to either 1) download automatically or 2) browser to throw 'Open or Save as' dialog. I'm using Icefaces to render the link but this doesn't matter, I can use plain html with javascript or jquery. Here is my code.

<ice:outputLink id="location" value="#{bean.downloadLink}" type="application/x-download" target="_blank">
        <ice:outputText value="Download"/>
</ice:outputLink>

Please note that I'm not streaming the file content through http response. If I did that I could have easily set content type and content disposition tags to achieve what I wanted. What I really want is a way to set these headers while rendering static resources as direct links.

Was it helpful?

Solution

Just provide an URL with a valid extension and register if necessary the extension with a content type by <mime-mapping> in web.xml.

E.g. the following link which is ultimately produced by JSF regardless of components you use:

<a href="file.csv">Download</a>

with

<mime-mapping>
    <extension>csv</extension>
    <mime-type>text/csv</mime-type>
</mime-mapping>

Note that the servlet container has by default already a whole list of mime mappings in its own web.xml, among others CSV. Only for example OpenXML Office documents (docx, xlsx, etc) and HTML5 image formats (SVG) may need to be registered manually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top