Here's what I've been using, sorry for the lazy response. I had to run.
try{
javax.servlet.ServletOutputStream out =
cycle.getRequestContext().getResponse().getOutputStream();
java.io.File file = new
java.io.File(work.getFileURIString());
java.lang.String contentType =
cycle.getRequestContext().getServlet().getServletContext().getMimeType(file.toString());
if (contentType != null){
cycle.getRequestContext().getResponse().setContentType(contentType);
} else{
cycle.getRequestContext().getResponse().setContentType("application/octet-stream");
}
cycle.getRequestContext().getResponse().setContentLength((int)file.length());
cycle.getRequestContext().getResponse().setHeader("Content-Disposition",
"attachment; filename=\"" + work.getTitle() + file.getName() + "\"");
java.io.FileInputStream in = new
java.io.FileInputStream(file);
byte[] buf = new byte[4 * 1024];
int bytesRead;
while ((bytesRead = in.read(buf)) != -1){
out.write(buf, 0, bytesRead);
}
out.flush();
out.close();
}catch(java.io.IOException e){
System.out.println(e.toString());
}
Sebastian Knoerzer wrote:
>hi,
>
>i have the following problem.
>i have an asset (a pdf file) and i want to download that if the user
>clicks on a directlink, but i always get a filenotfoundexception
>if i try to generate the fileinputstream-object.
>
>code:
>
>public void downloadFileAction(IRequestCycle cycle) {
> try {
> HttpServletResponse response =
> cycle.getRequestContext().getResponse();
> IAsset asset = getAsset("downloadFile");
> File file = new File(asset.getResourceLocation().getPath());
> System.out.println(file.length());
> FileInputStream fileInputStream = new FileInputStream(file);
> BufferedInputStream bufferedInputStream = new
>BufferedInputStream(fileInputStream);
>
> int size = (int) file.length();
> byte[] data = new byte[size];
> bufferedInputStream.read(data, 0, size);
> bufferedInputStream.close();
>
> response.setContentType("application/pdf");
> response.setHeader("Content-disposition",
>"inline; filename=" + getCurrentFile().getFileName());
> response.setContentLength(size);
>
> ServletOutputStream out = response.getOutputStream();
> out.write(data);
>
> response.flushBuffer();
> out.close();
> }
> catch (IOException e) {
> e.printStackTrace();
> }
> }
>
>
>
>page file:
>...
><context-asset name="downloadFile" path="uploads/test.pdf"/>
>...
>
>so does anyone know how to download a file to the user?
>
>
>thanks for your help!
>
>regards
>
>
>sebastian
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
--
Justin Stanczak
Stanczak Group
812-735-3600
"All that is necessary for the triumph of evil is that good men do nothing."
Edmund Burke
..________...............__.................
./ _____/..____..._____/..|_..____...____....
/...\..____/.__.\./....\...__\/.._.\./._..\....
\....\_\..\..___/|...|..\..|.(..<_>.|.<_>..)....
.\______../\___.._\__|../__|..\____/.\____/......
........\/.....\/.....\/..........................
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]