67,513
社区成员
发帖
与我相关
我的任务
分享
items: ['->',{
text: '导出',
handler: function(){
Ext.MessageBox.confirm('提示','确定要导出网点吗?',function(btn){
var requestObj = gridFormPanel.form.getValues();
if (btn == "yes") {
var form = gridFormPanel.form;
var requestObj = gridFormPanel.form.getValues();
form.submit({
waitMsg:"数据处理中,请等待...",
// params:Ext.JSON.encode(requestObj),
url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
method: 'POST'
}
})
}
}]
String fileName = result.getString("path");
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
ServletOutputStream sos = response.getOutputStream();
// PrintWriter writer = response.getWriter();
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(fileName));
byte[] content = new byte[1024];
int length;
while ((length = fin.read(content, 0, content.length)) != -1) {
sos.write(content, 0, length);
}
fin.close();
sos.flush();
sos.close();
// writeResponse(response, result.toString());
form.submit({
// waitMsg:"数据处理中,请等待...",
// params:Ext.JSON.encode(requestObj),
url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
method: 'POST',
success : function(form, action) {
alert(action.result.msg);
alert(action.response.responseText);
// var obj = Ext.JSON.decode(action.response.responseText);
// alert(obj.path);
// window.open(obj.path, '_blank',
// 'width=1,height=1,toolbar=no,menubar=no,location=no');//报错,提示“拒绝访问”
},
failure : function(form, action) {
alert(action.failureType);
alert(action.result.msg);
alert(action.response.responseText);
alert("failure");
}
});
所以不能设置success的值,直接返回到failure里了,然后在failure里应该怎么写?var gridFormPanel = Ext.create('Ext.FormPanel', { standardSubmit:true})
,这样表单提交会以正常方式提交,就会弹出下载框了,搞了我2天了,心都肿了,现在贴出我的代码
String fileName = result.getString("path");
File file = new File("E://"+ fileName);
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition", "attachment; filename="+ fileName);
OutputStream out = response.getOutputStream();
BufferedInputStream buff = new BufferedInputStream(new FileInputStream(file));
byte[] content = new byte[1024];
long k = 0;
while(k<file.length()){
int j=buff.read(content,0,1024);
k+=j;
out.write(content,0,j);
}
out.flush();
out.close();
buff.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
js代码
var gridFormPanel = Ext.create('Ext.FormPanel', { standardSubmit:true}
handler: function(){
Ext.MessageBox.confirm('提示','确定要导出网点吗?',function(btn){
var requestObj = gridFormPanel.form.getValues();
if (btn == "yes") {
var form = gridFormPanel.form;
var requestObj = gridFormPanel.form.getValues();
form.submit({
// waitMsg:"数据处理中,请等待...",
// params:Ext.JSON.encode(requestObj),
url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
method: 'POST'
});
}
})
}
贴出来后希望对别人有帮助,我真是被搞死了 resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition", "attachment; filename=file.xls");
OutputStream os = resp.getOutputStream();
FileInputStream in = new FileInputStream(new File("xxxxx"));
int n = 0;// 每次读取的字节长度
byte[] bb = new byte[1024];// 存储每次读取的内容
while ((n = in.read(bb)) != -1) {
os.write(bb, 0, n);// 将读取的内容,写入到输出流当中
}
os.close();// 关闭输入输出流
in.close();
form.submit({
waitMsg:"数据处理中,请等待...",
// params:Ext.JSON.encode(requestObj),
url: THREETI.EXPORT_FILE_URL + "sName=/exportSuperItemExcel&&shopCategory=0",
method: 'POST',
success : function(form, action) {
alert(action.response.responseText);
// var obj = Ext.JSON.decode(action.response.responseText);
// alert(obj.path);
// window.open(obj.path, '_blank',
// 'width=1,height=1,toolbar=no,menubar=no,location=no');//报错,提示“拒绝访问”
},
failure : function(form, action) {
//window.open('test.html','newwindow','height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')
alert(action.response.responseText);
alert("failure");
}
});
这里直接进入了failure,responseText里有很长一串乱码的东西