• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save windows?

 
Suka Hati
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how i want to make a popup windows so that when there a link to a file in server...i will pop-up a windows ask whether user want to open or save the file
[ November 29, 2004: Message edited by: Bear Bibeault ]
 
Anitha Krishnamurthi
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
call a jsp or servlet on click of the link to the file and write below code on the destination jsp or servlet

String fileName = httpservletrequest.getParameter("filename");
String dirName = httpservletrequest.getParameter("dirname");
int dotIndex = fileName.lastIndexOf(".");
String fileType = fileName.substring(dotIndex+1,fileName.length());
ServletOutputStream out = httpservletresponse.getOutputStream();
if (fileType.trim().equalsIgnoreCase("doc"))
{
httpservletresponse.setContentType( "application/msword" );
}
else if (fileType.trim().equalsIgnoreCase("xls"))
{
httpservletresponse.setContentType( "application/vnd.ms-excel" );
}
else if (fileType.trim().equalsIgnoreCase("pdf"))
{
httpservletresponse.setContentType( "application/pdf" );
}
else if (fileType.trim().equalsIgnoreCase("ppt"))
{
httpservletresponse.setContentType( "application/ppt" );
}
else
{
httpservletresponse.setContentType( "application/octet-stream" );
}

httpservletresponse.setHeader("Content-disposition", "attachment; filename=" +fileName);
String filePath = dirName+"/"+fileName;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
BufferedOutputStream bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{
bos.write(buff, 0, bytesRead);
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic