Problem with fileDownload

UI Components for JSF
Post Reply
rasmusson.stefan
Posts: 22
Joined: 26 Mar 2010, 16:00

07 May 2010, 12:50

I'm trying to use the fileDownload component to download a generated XML file

Code: Select all

@ManagedBean(name = "DownloadTokenController")
@SessionScoped
public class DownloadTokenController {

	@EJB
	private UserDataService userDataService;
	
	private StreamedContent file;
	
	@PostConstruct
	public void init() {
		UserData userData = userDataService.getUserData();
		XMLOutputter printer = new XMLOutputter(Format.getPrettyFormat());
		
		InputStream in = IOUtils.toInputStream(printer.outputString(userDataService.exportAccessTooken(userData)));
		
		setFile(new DefaultStreamedContent(in, "text/xml", "token." + "extension"));
	}
	public void setFile(StreamedContent file) {
		this.file = file;
	}
	public StreamedContent getFile() {
		System.out.println("DownloadTokenController getFile");
		return file;
	}
IOUtils is Apache commons io.

I use this to try to download the file.

Code: Select all

<p:commandButton value="Velg rolle" id="selectRole" action="#{ChooseExportController.download}">
 	<p:fileDownload value="#{DownloadTokenController.file}" contentDisposition="attachment"/>
</p:commandButton>
The ChooseExportController.download only sets some uninsteresting variables

When I click the button nothing happends, no error message nothing.
What could be the problem? Is there any other good way to download generated files using JFS?

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

09 May 2010, 09:56

So DownloadTokenController getFile message is not displayed? Do you have any validations on page? Can you post full page? Maybe there is a validation error.

rasmusson.stefan
Posts: 22
Joined: 26 Mar 2010, 16:00

10 May 2010, 09:25

The message is displayed. I see now that I get a response on the post in firebug, the response contains exactly the data that should be in the file. But I don't get it on the screen and I son't get a file

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

10 May 2010, 13:03


rasmusson.stefan
Posts: 22
Joined: 26 Mar 2010, 16:00

10 May 2010, 13:59

Yes.

rasmusson.stefan
Posts: 22
Joined: 26 Mar 2010, 16:00

12 May 2010, 15:27

My response header looks like this

HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0, JSF/2.0
Server: GlassFish v3
Content-Disposition: attachment;filename=token.extension
Content-Type: text/xml
Transfer-Encoding: chunked
Date: Wed, 12 May 2010 13:18:40 GMT


Does it look ok to you?

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

12 May 2010, 17:27

Yes, you export xml right? And content type is text/xml. I can't really figure out what the problem is.

rasmusson.stefan
Posts: 22
Joined: 26 Mar 2010, 16:00

26 May 2010, 10:45

It figured out the problem.
I was using the primefaces commad button that does a partial ajax submit. Changed to jsf commandbutton and everything works

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

29 May 2010, 12:06

Glad it works now, I should have seen that from the p:commandButton code. Following could work as well by setting ajax to false;

Code: Select all

<p:commandButton value="Velg rolle" id="selectRole" action="#{ChooseExportController.download}" ajax="false">
   <p:fileDownload value="#{DownloadTokenController.file}" contentDisposition="attachment"/>
</p:commandButton>

lurajon
Posts: 8
Joined: 20 May 2011, 10:06

10 Jun 2011, 10:51

Hi,

I get a similar problem with fileDownload. But none of the solutions works. I have tried with h:commandButton and p:commandButton ajax=false

I am using JSF 2.0, Primefaces 3.0.1M and Icefaces 2. (Icefaces stuff in web.xml, but not used in this specific page)

The file is generated on the server side, but it fails to start download in client.

Firebug response shows me the correct data. But there is an error message in the console.

Code: Select all

[window] Error [status: emptyResponse code: 200]: An empty response was received from the server. Check server error logs.
(?)(self=self(operator), category=["window"], message="Error [status: emptyRes...heck server error logs.", exception=undefined)bridge...8784371 (line 1224)
operator()bridge...8784371 (line 184)
(?)(self=self(operator), operation=function(), category=["window"], message="Error [status: emptyRes...heck server error logs.", exception=undefined)bridge...8784371 (line 1264)
operator()bridge...8784371 (line 184)
Logger(self=self(operator), message="Error [status: emptyRes...heck server error logs.", exception=undefined)bridge...8784371 (line 1182)
operator()bridge...8784371 (line 184)
(?)(e=Object { type="error", status="emptyResponse", more...})bridge...8784371 (line 2092)
ajax(request=XMLHttpRequest { responseText="bytes[........], mozResponseArrayBuffer=ArrayBuffer, more...}, context=Object { sourceid=button#cellKeyManagementForm:download.ui-button, onerror=false, more...}, status="emptyResponse", description=undefined, serverErrorName=undefined, serverErrorMessage=undefined)jsf.js...x.faces (line 1)
ajax(request=XMLHttpRequest { responseText="bytes[...........]", mozResponseArrayBuffer=ArrayBuffer, more...}, context=Object { sourceid=button#cellKeyManagementForm:download.ui-button, onerror=false, more...})jsf.js...x.faces (line 1)
ajax()jsf.js...x.faces (line 1)
ajax()jsf.js...x.faces (line 1)
exception ? console.er...ror(formatOutput(category, message)); 
If I change the content to text/xml instead of application/octet-stream the javascript gives me xml parsing error (can't find element of some kind)

Any ideas?

Best regards
Jone Lura

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 47 guests