save file on client

hi!there must be a solution to this problem! can anybody tell?i want du save text in a file on the client, how to do?thanks a lot!
[158 byte] By [schaeflia] at [2007-11-27 6:45:43]
# 1
Just stream it to the response as attachment.You may find the downloadFile() snippets at the bottom of this article useful: http://balusc.xs4all.nl/srv/dev-jep-pdf.html
BalusCa at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
thank you very much!that is exactly what i want!
schaeflia at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

i've got an error!

Cannot forward after response has been committed!

try {

downloadFile (((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()),new File(path));

getFacesContext().getCurrentInstance().responseComplete();

} catch (IOException ex) {

ex.printStackTrace();

}

and the download-method:

public static void downloadFile(

HttpServletResponse response, File file)

throws IOException

{

BufferedInputStream input = null;

BufferedOutputStream output = null;

try {

// Wrap the file in a BufferedInputStream and pass it through another method.

input = new BufferedInputStream(new FileInputStream(file));

//downloadFile(response, input, file.getName(), attachment);

int contentLength = input.available();

String contentType = URLConnection.guessContentTypeFromName(file.getName());

response.setContentLength(contentLength);

response.setContentType(contentType);

response.setHeader(

"Content-disposition", "attachment" + "; filename=\"" + file.getName() + "\"");

output = new BufferedOutputStream(response.getOutputStream());

// Write file contents to response.

while (contentLength-- > 0) {

output.write(input.read());

}

output.flush();

} catch (IOException e) {

throw e;

} finally {

if (input != null) {

try {

input.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (output != null) {

try {

output.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

schaeflia at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
The method should be declared void and you should not handle the navigation during download.
BalusCa at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
isn't the method void?i want to do the download after clicking on a button.in this action event i want to download and refresh my tree-component. the returnvalue is null!
schaeflia at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
I was talking about the action method. You can send only one response at once. And in case of a file download this should be the actual download file. You cannot send another response afterwards.
BalusCa at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
the action event return value is void now. There isn't an error anymore, but i need the page to be refreshed!
schaeflia at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
You cannot send two responses for one request. Consider opening a new window using JS which independently loads the download.
BalusCa at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
is JS=javascript?i don't know javascript...
schaeflia at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

main JSF<h:commandButton value="download file" action="#{myBean.action}" onclick="window.open('download.jsf');" />

download.jsf<%

mypackage.MyBean myBean = (mypackage.MyBean) session.getAttribute("myBean");

myBean.downloadFile();

%>

<html>

<head><title>Download</title></head>

<body>Downloading ..</body>

</html>

MyBean (should be session scoped)public void action() {

// Do your thing to handle main JSF during download.

}

public void downloadFile() {

// Stream file to response of download.jsf.

}

BalusCa at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
i've made it with an other button only for downloading, it works now, thank you
schaeflia at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
can i check somehow in the download-dialog if the user has clicked cancel?
schaeflia at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

What does session.getAttribute("myBean"); do in this example?

Do I have to declare "myBean" somewhere before this line?

When I try to use the code I am getting a sendError(SRTServletResponseContext.java:162)

The first page is working fine but the pop up one shows as an error.

Cheers,

Illu

Illua at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
The "myBean" should be the name of a session scoped managed bean of which it's class is "mypackage.MyBean".Also see the downloadFile() snippets at the bottom of this article: http://balusc.xs4all.nl/srv/dev-jep-pdf.html
BalusCa at 2007-7-12 18:17:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

Got it, thanks!

I've debgged through it and it's going to the right method and everything but it throws an error at the line

if (!ctx.getResponseComplete()) {

And also when I comment this out, the next line throws an error:

HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();

I think it could be something with the FacesContext ctx? Any ideas?

Illu

Illua at 2007-7-21 22:05:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
There are countless kinds of errors. Which one are you talking about?
BalusCa at 2007-7-21 22:05:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...