• Post Reply Bookmark Topic Watch Topic
  • New Topic

Downloading file with option for users to save or open the file  RSS feed

 
Nilesh Chokhadia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the syntax of downloading any file from server to client machine using <a href="folename">click</a> this code directly gives a save file option but how to give a option to user to open it or save it using response.setContentType("text/csv"); option please try to explain with an example code.
Regards
Nilesh B Chokhadia
 
Joe Pardi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of setting the content type to "text/csv", try "octet/stream".
 
Nilesh Chokhadia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying
when i make <%response.setContentType("octet/stream");%>
while loading jsp page it gives me the option of save open or cacel for jsp file I want this option for downloading file like execel,word,wav format file so where can I place this <%response.setContentType("octet/stream");%>
should i palce with
<a href="ShiftScheduled.xls" ....onCclick="<%response.setContentType("octet/stream");%>">shift</a> but it never works help me.
regards
Nilesh
 
Joe Pardi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSP below will take a file named "test.doc" and stream it back to the web browser as an octet/stream (a bunch o unknown bytes). This forces the browser to open the "Open or Save As" dialog.
=========================================================================
<%@ page import="javax.servlet.*,java.io.*" %><%@ page contentType="octet/stream" %><%
response.addHeader("Content-Disposition", "filename=test.doc");
File f = new File("c:\test.doc"));
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[(int)f.length()];
fis.read(buffer,0,(int)f.length());
fis.close();
ServletOutputStream os = response.getOutputStream();
os.write(buffer);
os.close();
%>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!