Downloading file with option for users to save or open the file
- Post ReplyBookmark TopicWatch Topic
- New Topic
posted 16 years ago
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
Regards
Nilesh B Chokhadia
posted 16 years ago
Instead of setting the content type to "text/csv", try "octet/stream".
posted 16 years ago
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
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
posted 16 years ago
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();
%>
=========================================================================
<%@ 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();
%>
- Bookmark TopicWatch Topic
- New Topic
current ranch time (not your local time) is Dec 04, 2020 02:59:46
-
-
-