Only 49 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass 'Save as' for download?  RSS feed

 
Jim Wang
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, everyone:
I just got my download function work, so this time after user click on download button, it wouldn't open PDF file in the browser, in stead it will pop up a 'Save as' window. For some reason, we don't want our user to change the files location and file name. The solution is after user click download button, the server will set the feedback as 'Yes' then procede the download. Could anyone tell me how to do this.
I appriciate for any help?
Thanks.
Jim
 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post the code that you are using for save as download
function.
I am interested in this.,
appreciate
 
Mahesh Rana
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt, you can not directly start the download.
By the way, You can set the default file name in Save dialog.
response.setHeader("Content-Disposition",
"attachment;filename="
+ "file.name");
 
Jim Wang
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source Code:
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String pdf = req.getParameter("pdfurl");
String filename = pdf.substring(pdf.lastIndexOf("/")+1);

//res.setContentType("application/x-filler");
res.setContentType("application/pdf");
res.setHeader("Content-Disposition","attachment;filename=" + filename );


try{
FileInputStream fileInput = new FileInputStream(pdf);
int numOfBytes = fileInput.available();
byte byteArray[] = new byte[numOfBytes];
int nextByte = fileInput.read(byteArray);
fileInput.close();

OutputStream outStream = res.getOutputStream();
outStream.write(byteArray);
outStream.close();
}
catch(IOException ioe){
System.out.println("download:file input exception");//..........
//ioe.printStackTrace();
}
catch(Exception e){
System.out.println("download: exception");//..........
}
}
 
madhu gun
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
i have got a similar problem with downloading rtf
file.
I was reading an rtf file from local system(servlet system) and it was downloading fine in browser with correct name with save as dialog ofcourse my browser has rtf plugin.
But I have changed the code now as getting the file as a string from my ejb and writing the string to the out with setContentType and setContentHeader as it is.but now it directly opens in browser.
But if the file is procured by reading from file system by servlet,then the save as dialog does not have problem.
I tried using servletoutputstream.write(bytearray) also after conevrting the string into bytearray using getBytes.it doesn't work .Any ideas please.
regards
madhu
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!