4 Replies Latest reply: Jul 17, 2007 4:58 AM by Cui Michael RSS

    how to set the contenttype in reponse header under the ajax4

    Cui Michael Newbie

      I want to implement a download csv file from a button, but the header's content type was rewrited before it was send back to the client. It looks like the jsf or ajax4jsf framework rewrite the content type of the reponse header.

      Could someone tell me how to set the content type as I expected?

      Regards,

      hefeng

        • 1. Re: how to set the contenttype in reponse header under the a
          Sergey Smirnov Master

          I hope, you are not going to change the content type of ajax response. It is impossible by definition.

          • 2. Re: how to set the contenttype in reponse header under the a
            Cui Michael Newbie

            Hi, SergeySmirnov

            My case is to press a button to export some data as a CSV file in client side. Could you give me some suggestion about its implementation?

            Currently, I used the following way to do it, but the browser open the CSV file directly instead of showing a dialog to ask the download location.

             FacesContext facesContext = FacesContext.getCurrentInstance();
             HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse();
            
             String contentType = "application/octet-stream;charset=UTF-8";
             response.setContentType(contentType);
            
             String buffer ="aaa,bbb,ccc,ddd,eee,aaaaaaaaaaa";
             String filename ="my.csv";
            
             response.setHeader("Content-Disposition","attachment;filename=\""+ filename + "\"");
            
             OutputStream responseStream =
            ((HttpServletResponse)facesContext.getExternalContext().getResponse()).getOutputStream();
            
             if (null == responseStream) throw new AbortProcessingException("responseStream is null");
            
            
             responseStream.write(buffer.getBytes());
             responseStream.flush();
             responseStream.close();
            
             facesContext.responseComplete();
            


            The header of my response was as following

            Content-Type: application/x-www-form-urlencoded; charset=UTF-8
            Referer: http://localhost:8080/icc-statistics/pages/tree16.jsf
            Content-Length: 295
            Cookie: JSESSIONID=A44D0BB2517414C90957D3AD94D6978A
            Pragma: no-cache
            Cache-Control: no-cache
            AJAXREQUEST=_viewRoot&toolBarForm%3Afrom_date.day=&toolBarForm%3Afrom_date.month=-1&toolBarForm%3Afrom_date.year=&toolBarForm%3Ato_date.day=&toolBarForm%3Ato_date.month=-1&toolBarForm%3Ato_date.year=&com.sun.faces.VIEW=_id1%3A_id3&toolBarForm=toolBarForm&toolBarForm%3A_id41=toolBarForm%3A_id41&
            HTTP/1.x 200 OK
            Server: Apache-Coyote/1.1
            content-disposition: attachment;filename="my.csv"
            Content-Type: application/octet-stream;charset=UTF-8
            Content-Length: 31
            




            • 3. Re: how to set the contenttype in reponse header under the a
              Cui Michael Newbie

              Another way I want to try is forward the requestion from Back Bean to a servlet to handle the export action. But I also have the problem with getWriter from response.

              Does this way is possible? What's the right way to implement this case in Ajax4Faces framework?

              • 4. Re: how to set the contenttype in reponse header under the a
                Cui Michael Newbie

                I got some interesting result of my question.

                In order to show Open/Save dailog to download file from server, user should use

                <h:commandLink id="link1" value="download2" action="#{bean.download}" />

                rather than use
                <a4j:commandButton action="#{bean.download}" value="download1" />


                In the case of using <a4j:commandButton/> or <a4j:commandLink/>, the download file will be automatically opened by browser instead of showing Open/Save dialog.

                Here is my back bean.


                 public void download() throws IOException{
                
                 FacesContext context = FacesContext.getCurrentInstance();
                
                 HttpServletResponse response =
                 ( HttpServletResponse ) context.getExternalContext().getResponse();
                
                 String buffer = "aaaaaaaaaaaaa";
                
                 response.setContentType("application/x-download");
                
                 response.setHeader("Content-Disposition", "attachment;filename=\"" +
                 "aaa.csv" + "\"");
                
                
                 OutputStream os = null;
                
                 os = response.getOutputStream();
                
                 os.write(buffer.getBytes());
                
                
                 os.flush();
                 os.close();
                
                
                 FacesContext.getCurrentInstance().responseComplete();
                 }
                

                Referenced from http://wiki.apache.org/myfaces/Sending_Files

                Here is the HTTP Response's header of two tags:

                HTTP Response's header of using <h:commandLink/>
                
                POST /myapp/pages/download.jsf HTTP/1.1
                
                Host: localhost:8080
                
                User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.4) Gecko/20061201 Firefox/2.0.0.4 (Ubuntu-feisty)
                
                Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
                
                Accept-Language: en-us,en;q=0.5
                
                Accept-Encoding: gzip,deflate
                
                Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
                
                Keep-Alive: 300
                
                Connection: keep-alive
                
                Referer: http://localhost:8080/myapp/pages/download.jsf
                
                Cookie: JSESSIONID=18A9C87EA2BBEA2C298D51B59139ADE9
                
                Content-Type: application/x-www-form-urlencoded
                
                Content-Length: 66
                
                com.sun.faces.VIEW=_id1%3A_id2&_id2=_id2&_id2%3A_idcl=_id2%3Alink1
                
                HTTP/1.x 200 OK
                
                Server: Apache-Coyote/1.1
                
                content-disposition: attachment;filename="aaa.csv"
                
                Content-Type: application/x-download;charset=ISO-8859-1
                
                Content-Length: 13
                
                


                HTTP Response's header of using <a4j:commandButton/>
                
                POST /myapp/pages/download.jsf HTTP/1.1
                
                Host: localhost:8080
                
                User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.4) Gecko/20061201 Firefox/2.0.0.4 (Ubuntu-feisty)
                
                Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
                
                Accept-Language: en-us,en;q=0.5
                
                Accept-Encoding: gzip,deflate
                
                Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
                
                Keep-Alive: 300
                
                Connection: keep-alive
                
                Content-Type: application/x-www-form-urlencoded; charset=UTF-8
                
                Referer: http://localhost:8080/myapp/pages/download.jsf
                
                Content-Length: 101
                
                Cookie: JSESSIONID=18A9C87EA2BBEA2C298D51B59139ADE9
                
                Pragma: no-cache
                
                Cache-Control: no-cache
                
                AJAXREQUEST=_viewRoot&com.sun.faces.VIEW=_id1%3A_id2&_id2=_id2&_id2%3A_idcl=&_id2%3A_id3=_id2%3A_id3&
                
                HTTP/1.x 200 OK
                
                Server: Apache-Coyote/1.1
                
                content-disposition: attachment;filename="aaa.csv"
                
                Content-Type: application/x-download;charset=ISO-8859-1
                
                Content-Length: 13