FileDownload doesn't download the requested file

UI Components for JSF
Post Reply
claguna
Posts: 2
Joined: 17 Jun 2011, 02:42

20 Jun 2011, 01:58

Hi everybody.

I'm writing because I have an issue with fileDownload. I think I have configured correctly my xhtml and my ManagedBean; however, while I want to download a text file the downloaded file is an html, e.g.

Code: Select all

Downloaded file
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<link type="text/css" rel="stylesheet" href="/AsimilacionLluvias/faces/javax.faces.resource/theme.css?ln=primefaces-bluesky" />
<link type="text/css" rel="stylesheet" href="/AsimilacionLluvias/faces/javax.faces.resource/jquery/ui/jquery-ui.css?ln=primefaces&v=3.0.M1" />
<script type="text/javascript" src="/AsimilacionLluvias/faces/javax.faces.resource/jquery/jquery.js?ln=primefaces&v=3.0.M1"></script>
<script type="text/javascript" src="/AsimilacionLluvias/faces/javax.faces.resource/jquery/ui/jquery-ui.js?ln=primefaces&v=3.0.M1"></script>
<script type="text/javascript" src="/AsimilacionLluvias/faces/javax.faces.resource/core/core.js?ln=primefaces&v=3.0.M1"></script>
<script type="text/javascript" src="/AsimilacionLluvias/faces/javax.faces.resource/button/button.js?ln=primefaces&v=3.0.M1"></script>
<script type="text/javascript" src="/AsimilacionLluvias/faces/javax.faces.resource/imageswitch/imageswitch.js?ln=primefaces&v=3.0.M1"></script>
        <title>Asimilacion</title></head><body><span style="width: 960px; border:0; ">
            <img src="img/logomatemP.jpg" align="left" />
            <img src="img/logounamP.jpg" align="right" /></span>
        <br /><br /><br /><br /><br /><br /><br /><br /><br />
        
            <div>
<form id="j_idt9" name="j_idt9" method="post" action="/AsimilacionLluvias/faces/displayImages.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt9" value="j_idt9" />
<button id="j_idt9:j_idt10" name="j_idt9:j_idt10" onclick=";" type="submit">Descargar Datos</button><script type="text/javascript">widget_j_idt9_j_idt10 = new PrimeFaces.widget.CommandButton('j_idt9:j_idt10', {});</script><button id="j_idt9:j_idt11" name="j_idt9:j_idt11" onclick=";" type="submit">Descargar Imagenes</button><script type="text/javascript">widget_j_idt9_j_idt11 = new PrimeFaces.widget.CommandButton('j_idt9:j_idt11', {});</script><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="8824114433664614171:-1175310793849761689" autocomplete="off" />
</form>
            </div>
            <div><button id="j_idt13" name="j_idt13" onclick="switcher.previous();" type="button">ui-button</button><script type="text/javascript">widget_j_idt13 = new PrimeFaces.widget.CommandButton('j_idt13', {text:false,icons:{primary:'ui-icon ui-icon-circle-triangle-w'}});</script><button id="j_idt14" name="j_idt14" onclick="switcher.next();" type="button">ui-button</button><script type="text/javascript">widget_j_idt14 = new PrimeFaces.widget.CommandButton('j_idt14', {text:false,icons:{primary:'ui-icon ui-icon-circle-triangle-e'}});</script><div id="j_idt15"><img id="j_idt16:0:j_idt17" src="Data/2007_10_16_3/2007_10_16_3.eps.jpeg" alt="" /></div><script type="text/javascript">jQuery(function() {switcher = new PrimeFaces.widget.ImageSwitch('j_idt15',{fx:'fade',speed:500,timeout:0});});</script>              
                </div></body>
</html>
Bean

Code: Select all


    @PostConstruct
public void postConstruct() {
   InputStream stream = null;
        {
             StringBuilder sb = new StringBuilder();
            BufferedReader reader = null;
            try {
                ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
                String deploymentDirectoryPath = ctx.getRealPath("/");
                stream = new  FileInputStream(new File(deploymentDirectoryPath+"Data/2007_10_16_3/2007-10-16_3.a1.debug"));
                String f = stream.toString();
                reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                while ((line = reader.readLine()) != null) {

                    sb.append(line).append("\n");
                }
                file = new DefaultStreamedContent(stream, "text/plain", "2007-10-16_3.a1.debug");
            } catch (IOException ex) {
                Logger.getLogger(DownloadDataBean.class.getName()).log(Level.SEVERE, null, ex);
            }  finally {
                try {
                    stream.close();
                } catch (IOException ex) {
                    Logger.getLogger(DownloadDataALBean.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(DownloadDataBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
}
    
    /** Creates a new instance of DownloadData */
    public DownloadDataBean() {
           
    }
XHTML

Code: Select all

<h:form>
                    <p:commandButton value="Descargar Datos"  ajax ="false" >
                        <p:fileDownload value="#{downloadDataBean.file}" />  
                    </p:commandButton>
                       
                
                    <p:commandButton value="Descargar Imagenes"  ajax ="false" />
                    </h:form>

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 25 guests