前台用ajax传文件名到后台,struts2接收,并下载,但是页面并没有弹出保存另存为的那个窗口,为什么? 5
代码如下:前台Ext.Ajax.request({url:'chat/downloadfile.action',method:'post',params:{name:na...
代码如下:
前台
Ext.Ajax.request({
url:'chat/downloadfile.action',
method:'post',
params:{name:name},
success:function(response,option){
Ext.Msg.alert("消息提示","预览成功");
},
failure:function(response,option){
Ext.Msg.alert("消息提示","预览失败");
}
});
后台:
public void downLoadFile() throws IOException {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String filePath = request.getSession().getServletContext().getRealPath("/") + "uploadFiles\\";
String fileName = request.getParameter("name");
FileInputStream fs = null;
filePath = filePath + fileName;
try {
fs = new FileInputStream(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//设置响应头和保存文件名
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" +
fileName + "\"");
//写出流信息
int b = 0;
try {
PrintWriter out = response.getWriter();
while ((b = fs.read()) != -1) {
out.write(b);
}
fs.close();
out.close();
System.out.println("文件下载完毕.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("下载文件失败!");
}
} 展开
前台
Ext.Ajax.request({
url:'chat/downloadfile.action',
method:'post',
params:{name:name},
success:function(response,option){
Ext.Msg.alert("消息提示","预览成功");
},
failure:function(response,option){
Ext.Msg.alert("消息提示","预览失败");
}
});
后台:
public void downLoadFile() throws IOException {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String filePath = request.getSession().getServletContext().getRealPath("/") + "uploadFiles\\";
String fileName = request.getParameter("name");
FileInputStream fs = null;
filePath = filePath + fileName;
try {
fs = new FileInputStream(new File(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//设置响应头和保存文件名
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" +
fileName + "\"");
//写出流信息
int b = 0;
try {
PrintWriter out = response.getWriter();
while ((b = fs.read()) != -1) {
out.write(b);
}
fs.close();
out.close();
System.out.println("文件下载完毕.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("下载文件失败!");
}
} 展开
展开全部
弹出保存另存为,是给前台看的
你把要下载的东西放在web-app下,然后页面上链接到这个文件,
页面上点就会出现的
你把要下载的东西放在web-app下,然后页面上链接到这个文件,
页面上点就会出现的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用ajax 形式不行,要用form submit 或者 window.href.location 这种方式才会弹出下载页面
追问
为什么我那个下载文件打开以后是空白的 什么都没有啊
追答
后台 应该有execption。或者流中就没有东西
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询