• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
%>
 
reply
    Bookmark Topic Watch Topic
  • New Topic