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?

asked 17.01.2014 at 17:53

jschori's gravatar image

jschori
(suspended)
accept rate: 100%

edited 18.02.2014 at 17:01

Flavio%20Sadeghi's gravatar image

Flavio Sadeghi ♦♦
(suspended)


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.

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:

<mime-mapping>
    <extension>docx</extension>
    <mime-type>application/vnd.openxmlformats-officedocument.wordprocessingml.document</mime-type>
</mime-mapping>
link

answered 20.01.2014 at 09:40

Reto%20Weiss's gravatar image

Reto Weiss ♦♦
4.9k202857
accept rate: 74%

You can find out the mimeType of e File in a Java Function.

    File f; // Set your File to f 
    HttpServletRequest request;
    request = (HttpServletRequest)
                 FacesContext.getCurrentInstance().getExternalContext().getRequest();

    String type = request.getSession().getServletContext().getMimeType(fileName)

Note: File f is a java.io.File you must convert the ivyFile to a javaFile

If you want to stream the File with <p:fileDownload> you need org.primefaces.model.DefaultStreamedContent;

The Final function, which you can call in a Ivy-ScriptStep, will look something like that

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;
link

answered 21.01.2014 at 14:55

Raphael%20B%C3%BCrgin's gravatar image

Raphael Bürgin
(suspended)
accept rate: 55%

edited 21.01.2014 at 14:55

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×32
×1

Asked: 17.01.2014 at 17:53

Seen: 20,064 times

Last updated: 18.02.2014 at 17:01