<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How to set content type of a download of a file?</title><link>https://answers.axonivy.com/questions/261/how-to-set-content-type-of-a-download-of-a-file</link><description>&lt;p&gt;I generate a temporary word document "docx" in the ivy files\session\ directory. 
Now I when the user click on a download link, the browser recognize the data as zip. 
How can I set the content type in the response?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/261/how-to-set-content-type-of-a-download-of-a-file" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 21 Jan 2014 14:55:16 -0500</lastBuildDate><item><title>Answer by Raphael Bürgin</title><link>https://answers.axonivy.com/questions/261/how-to-set-content-type-of-a-download-of-a-file/272</link><description>&lt;p&gt;You can find out the mimeType of e File in a Java Function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    File f; // Set your File to f 
    HttpServletRequest request;
    request = (HttpServletRequest)
                 FacesContext.getCurrentInstance().getExternalContext().getRequest();

    String type = request.getSession().getServletContext().getMimeType(fileName)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; File f is a java.io.File you must convert the ivyFile to a javaFile&lt;/p&gt;
&lt;p&gt;If you want to stream the File with &lt;code&gt;&amp;lt;p:fileDownload&amp;gt;&lt;/code&gt; you need &lt;code&gt;org.primefaces.model.DefaultStreamedContent;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The Final function, which you can call in a Ivy-ScriptStep, will look something like that&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public StreamedContent getStreamedConentFromFile(File file){

    StreamedContent fileDownloaded = null;
try {
       HttpServletRequest request;
       request = (HttpServletRequest) FacesContext.getCurrentInstance()
                     .getExternalContext().getRequest();
       InputStream inputStream = new FileInputStream(file);
       String fileName = file.getName();
       fileDownloaded = new DefaultStreamedContent(inputStream,
                     request.getSession().getServletContext()
                           .getMimeType(fileName), fileName);
       return fileDownloaded;
} catch (Exception e) {
       Ivy.log().debug("Fehler in setStreamedContentFromFile : " + e.getMessage());
       fileDownloaded = null;
}

file.delete();
file.deleteOnExit();

return fileDownloaded;
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Raphael Bürgin</dc:creator><pubDate>Tue, 21 Jan 2014 14:55:16 -0500</pubDate><guid>https://answers.axonivy.com/questions/261/how-to-set-content-type-of-a-download-of-a-file/272</guid></item><item><title>Answer by Reto Weiss</title><link>https://answers.axonivy.com/questions/261/how-to-set-content-type-of-a-download-of-a-file/268</link><description>&lt;p&gt;The content type of the file is set automatically by Xpert.ivy by analyzing the file extension. The content type of a file extension is searched first in the cms type system. If it is not found there it is searched in the web container. And finally in a hardcoded list in Xpert.ivy. &lt;/p&gt;
&lt;p&gt;In Xpert.ivy 5.0 the docx extension is known and the content type should be set automatically. In earlier version the docx extension is not known. You can add the following xml snipet to the webapps/ivy/WEB-INF/web.xml file so that the web container knows the extension and the content type of it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;mime-mapping&amp;gt;
    &amp;lt;extension&amp;gt;docx&amp;lt;/extension&amp;gt;
    &amp;lt;mime-type&amp;gt;application/vnd.openxmlformats-officedocument.wordprocessingml.document&amp;lt;/mime-type&amp;gt;
&amp;lt;/mime-mapping&amp;gt;
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reto Weiss</dc:creator><pubDate>Mon, 20 Jan 2014 09:40:20 -0500</pubDate><guid>https://answers.axonivy.com/questions/261/how-to-set-content-type-of-a-download-of-a-file/268</guid></item></channel></rss>