[RESOLVIDO]<p:fileDownload entra executa FileDownloadController

3 respostas
E

Pessoal, fiz o exemplo do site do prime, porem nao sei pq nao entra no noe metodo FileDownloadController, segue abaixo meu codigo:

<h:head>
        <title>Login Principal</title>
        
                <script type="text/javascript">  
function start() {  
    statusDialog.show();  
}  
  
function stop() {  
    statusDialog.hide();  
}  
        </script>  
    </h:head>

<h:form id="form">           
		<p:commandButton id="downloadLink" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)"   
			icon="ui-icon-arrowthichk-s">  
		    <p:fileDownload value="#{meuBean.file}" />  
		</p:commandButton>
</h:form>   


@ManagedBean(name="meuBean")
@ViewScoped
public class MeuBean implements Serializable{
             private String destination="C://Temp//";//aqui vc seta onde quer gravar o arquivo  
 private StreamedContent file;  

            
    public void FileDownloadController() {          
        InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");  
        file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");  
    }  


    public StreamedContent getFile() {
        
        return file;
    }

3 Respostas

G
@ManagedBean(name = &quot;fileDownloadController&quot;)
@ViewScoped
public class FileDownloadController {

	private StreamedContent file;

	public FileDownloadController() {
		InputStream stream = ((ServletContext) FacesContext
				.getCurrentInstance().getExternalContext().getContext())
				.getResourceAsStream(&quot;/images/downloaded_optimus.jpg&quot;);
		file = new DefaultStreamedContent(stream, &quot;image/jpg&quot;,
				&quot;downloaded_optimus.jpg&quot;);
	}

	public StreamedContent getFile() {
		return file;
	}
}
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;title&gt;Login Principal&lt;/title&gt;

&lt;script type="text/javascript"&gt;
	function start() {
		statusDialog.show();
	}

	function stop() {
		statusDialog.hide();
	}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;h:form id="form"&gt;
		&lt;p:commandButton id="downloadLink" value="Download" ajax="false"
			onclick="PrimeFaces.monitorDownload(start, stop)"
			icon="ui-icon-arrowthichk-s"&gt;
			&lt;p:fileDownload value="#{fileDownloadController.file}" /&gt;
		&lt;/p:commandButton&gt;
	&lt;/h:form&gt;
&lt;/body&gt;
&lt;/html&gt;

Crie um diretório em WebContent: /images/

[]'s

E

Fiz o que falou e funcionou, porem gostaria de fazer um download em um determinado diretorio por exemplo :
O stream esta vindo nullo

InputStream stream = ((ServletContext) FacesContext  
                .getCurrentInstance().getExternalContext().getContext())  
                  .getResourceAsStream("file:///C:/arquivo/votup/tela1621052013094032.jpg");  
                
        file = new DefaultStreamedContent(stream, "C:\\arquivo\\votup\\jpg",  
                "tela1621052013094032.jpg");
E

Resolvi assim :

public StreamedContent getFile() throws FileNotFoundException {  
        String caminho = "C:/arquivo/votup/tela1621052013094032.jpg";
        String arquivo = "tela1621052013094032.jpg";
        FileInputStream stream = new FileInputStream(caminho);    
        file = new DefaultStreamedContent(stream, caminho,arquivo); 
        return file;  
    }
Criado 21 de maio de 2013
Ultima resposta 21 de mai. de 2013
Respostas 3
Participantes 2