我有一个要求,比如当我点击"提交"按钮,我应该有一个小窗口,其中包含按钮,如"打开","保存"并且"取消"。我将展示一张图片以便更好地理解。
正如你可以看到上面的一个弹出窗口,它的问题就像打开"或者"保存"。我尝试过使用不同的代码,但没有得到我想要的内容。这是我的代码。
searchResult3.jsp
<script type="text/javascript">
function generateReport() {
var e = document.getElementById("idOfYourSelectYouNeedToAddedIt");
var strPage = e.options[e.selectedIndex].value;
window.open(strPage);
return false; //This make you stay on this page;
//return true; //Set the action tag in the form to the page you like to go to!
}
</script>
<body>
<form name="myForm" onsubmit="return generateReport()">
<select id = "idOfYourSelectYouNeedToAddedIt">
<option value=''> Make a selection </option>
<option value='sample2PDF.jsp'> PDF</option>
<option value='sample2XLS.jsp'> XLS </option>
<option value='sample2DOC.jsp'> DOC </option>
<option value='sample2XLSX.jsp'> XLSX </option>
</select>
<br/>
<input type="submit" value="Submit">
</form>
</body>
sampleDOC.jsp
<%@ page import="java.io.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Map"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="java.io.FileNotFoundException" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.engine.design.JRDesignQuery"%>
<%@ page import="net.sf.jasperreports.engine.xml.JRXmlLoader"%>
<%@ page import="net.sf.jasperreports.engine.design.JasperDesign"%>
<%@ page import="net.sf.jasperreports.view.JasperViewer"%>
<%@ page import="net.sf.jasperreports.engine.JRException"%>
<%@ page import="net.sf.jasperreports.engine.JasperCompileManager"%>
<%@ page import="net.sf.jasperreports.engine.JasperExportManager"%>
<%@ page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@ page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@ page import="net.sf.jasperreports.engine.JasperReport"%>
<%@ page import="net.sf.jasperreports.engine.export.ooxml.JRDocxExporter"%>
<%@ page import="javax.servlet.http.HttpServletResponse"%>;
<%@ page trimDirectiveWhitespaces="true"%>
<html>
<body>
<%
String sql=null;
Connection conn = null;
Object o2 = session.getAttribute("email");
String email = o2.toString();
String search=(String)session.getAttribute("search");//search
String search_tf=(String)session.getAttribute("search_tf");//companyname,contactperson,phone
if((search!=null && search.trim().length()>0) && (search_tf!=null && search_tf.trim().length()>0))
{
if(search.equals("Phone"))
{
sql="select * from lead where Phone like '" + search_tf.replaceAll("\\'","''") + "%' AND Email = '" + email + "'";
}
else if(search.equals("Contact Person"))
{
sql="select * from lead where Contact_Person like '" + search_tf.replaceAll("\\'","''") + "%' AND Email = '" + email + "'";
}
else
{
sql="select * from lead where Company_Name like '" + search_tf.replaceAll("\\'","''") + "%' AND Email = '" + email + "'";
}
}
else
{
sql="select * from lead where Email = '" + email + "'";
}
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
String jrxmlFile ="D:/dev/tools/jasper files/report10.jrxml";
InputStream input = new FileInputStream(new File(jrxmlFile));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("sql",sql);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,map,conn);
byte bytes[] = new byte[10000];
JRDocxExporter exporter = new JRDocxExporter();
ByteArrayOutputStream DOCStream = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, DOCStream);
exporter.exportReport();
//System.out.println("Size of byte array:"+DOCStream.size());
//bytes = DOCStream.toByteArray();
String fileName = "SalesReport.doc";
response.addHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
//response.setHeader("Content-Disposition", "inline; filename="
// + fileName);
response.setContentType("application/msword");
response.setContentLength(DOCStream.size());
//DOCStream.close();
OutputStream outputStream = response.getOutputStream();
DOCStream.writeTo(outputStream);
outputStream.flush();
//outputStream.close();
}
catch(Exception e)
{e.printStackTrace();}
%>
</body>
</html>
答案 0 :(得分:1)
就像@RoToRa所说,打开/保存对话框是Internet Explorer的一部分,它不是你可以在任意浏览器上触发的东西。
然而,当IE认为用户正在请求文件而不是网页(或浏览器可以显示的其他资源,如图像)时,会出现该对话框。浏览器会查看Content-Disposition
标题,作为何时显示此对话框的建议。
粗略地说,你应该服务
Content-Disposition: attachment; filename="SUGGESTED_FILE_NAME"
标头包含您的请求以触发对话框。有关详情,请参阅php - How to force download of a file?。