PDA

View Full Version : File Download



FireGlow
27 May 2009, 6:13 AM
Hey guys!

After it worked out to upload a file, I need to download it again!

The problem is there is no "real" file on the server (I delete it after storing some data in the database), I need to build it out of data from my database.

Now I want to click on a button and start the download dialog isntantly!

I tried to change the outputstream, and I got the the content of the file, but only as a kind of "error" :>


HttpServletResponse response = this.getThreadLocalResponse();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=\"" + "test.pdf\"");
File test = new File("D:/autoexec.cfg");

FileInputStream fis = null;
OutputStream os = null;
int read = 0;
byte[] bytes = new byte[1024];

// First we load the file in our InputStream
fis = new FileInputStream(test);
os = response.getOutputStream();
// While there are still bytes in the file, read them and write them
// to our OutputStream
while ((read = fis.read(bytes)) != -1) {
os.write(bytes, 0, read);
}

I read about a solution to add a hidden panel and redirect it to an existing file. But I don't want to create the file on the server, I just want to have the file in a stream.

Is that possible? If yes how?

thanks for helping

FireGlow
27 May 2009, 6:50 AM
Well,

I created a new Servlet (normal, not GWT) that writes my file into the response (like to upload it)!

I only wondering how to call it in a good way, because I just made a Form in this way:


public class ArchetypeDownloadForm extends FormPanel {

/**
*
*/
public ArchetypeDownloadForm() {

this.setAction(GWT.getModuleBaseURL() + "ArchetypeDownloadServlet?archetypeID=5");
this.setEncoding(Encoding.MULTIPART);
this.setMethod(Method.POST);

this.setVisible(false);
this.show();
RootPanel.get().add(this);
this.submit();
RootPanel.get().remove(this);
}
}

But I don't really like this way to call my servlet, what other optios do I have?