ajax response 导出文件
来源:互联网 发布:js cookie 跨域 编辑:程序博客网 时间:2024/06/01 10:16
前段
form隐藏域
<form id="form" style="display: none">
</form>
$.ajax({
url: createTxt/Excel, -- 在服务端生成文件
type:"POST",
async: false,
data:{data:queryData},
success: function (text) {
mini.unmask();
var result=mini.decode(text);
if(result.data.code =='0'){
expAjax(result.data.filePath,result.data.fileName);
}else{
mini.alert("导出出现异常");
return false;
}
},error:function(e){
mini.unmask();
mini.alert("导出出现异常。");
}
});
function expAjax(pathWj, wjmc) {
var param = '?pathWj=' + pathWj + '&wjmc=' + wjmc;
var form = $("#form"); //form表单
form.attr('target', '');
form.attr('method', 'post');
form.attr('action', downTxt/Excel + param);
form.submit(); //表单提交
}
@RequestMapping(value = "/downTxt/Excel ", method = RequestMethod.POST)
@ResponseBody
public ResultDTO downTxt/Excel (HttpServletRequest request,
HttpServletResponse response) {
ResultDTO dao = new ResultDTO();
try {
String pathWj = request.getParameter("pathWj");
String wjmc = request.getParameter("wjmc");
byte[] b1 = readFileByBytes(pathWj);
response.addHeader("Content-Disposition",
"attachment;filename=" + wjmc + ".xls"); --这里的文件名称要和生成的文件名称保持一致,不然下载不成功
response.setContentType("application/x-download");
response.getOutputStream().write(b1);
File file = new File(pathWj );
file.delete();
dao.setMessage("文件导出成功。");
} catch (Exception e) {
LOG.info("mesg", e);
dao.setMessage("导出失败。");
}
return dao;
}
public byte[] readFileByBytes(String fileName) {
InputStream in = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
in = new FileInputStream(fileName);
byte[] buf = new byte[1024];
int length = 0;
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
} catch (Exception e1) {
LOG.error("导出异常:" + e1.getMessage(), e1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
LOG.error(e1.getMessage(), e1);
}
}
}
return out.toByteArray();
}
form隐藏域
<form id="form" style="display: none">
</form>
$.ajax({
url: createTxt/Excel, -- 在服务端生成文件
type:"POST",
async: false,
data:{data:queryData},
success: function (text) {
mini.unmask();
var result=mini.decode(text);
if(result.data.code =='0'){
expAjax(result.data.filePath,result.data.fileName);
}else{
mini.alert("导出出现异常");
return false;
}
},error:function(e){
mini.unmask();
mini.alert("导出出现异常。");
}
});
function expAjax(pathWj, wjmc) {
var param = '?pathWj=' + pathWj + '&wjmc=' + wjmc;
var form = $("#form"); //form表单
form.attr('target', '');
form.attr('method', 'post');
form.attr('action', downTxt/Excel + param);
form.submit(); //表单提交
}
@RequestMapping(value = "/downTxt/Excel ", method = RequestMethod.POST)
@ResponseBody
public ResultDTO downTxt/Excel (HttpServletRequest request,
HttpServletResponse response) {
ResultDTO dao = new ResultDTO();
try {
String pathWj = request.getParameter("pathWj");
String wjmc = request.getParameter("wjmc");
byte[] b1 = readFileByBytes(pathWj);
response.addHeader("Content-Disposition",
"attachment;filename=" + wjmc + ".xls"); --这里的文件名称要和生成的文件名称保持一致,不然下载不成功
response.setContentType("application/x-download");
response.getOutputStream().write(b1);
File file = new File(pathWj );
file.delete();
dao.setMessage("文件导出成功。");
} catch (Exception e) {
LOG.info("mesg", e);
dao.setMessage("导出失败。");
}
return dao;
}
public byte[] readFileByBytes(String fileName) {
InputStream in = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
in = new FileInputStream(fileName);
byte[] buf = new byte[1024];
int length = 0;
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
} catch (Exception e1) {
LOG.error("导出异常:" + e1.getMessage(), e1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
LOG.error(e1.getMessage(), e1);
}
}
}
return out.toByteArray();
}
阅读全文
0 0
- ajax response 导出文件
- 使用Response导出GridView,下载文件
- 工作日记2017.11.14 response导出文件
- Ajax处理下载文件response没有反应
- Ajax处理下载文件response没有反应
- 利用ajax导出Excel文件
- 页面response将datatable导出成CSV文件
- 模仿ajax利用rest导出文件
- Ajax request/response 乱码
- response导出html到word
- ajax 导出文件( Excel, txt 等) 问题分析
- ajax 导出文件( Excel, txt 等) 问题分析
- EasyUi、SpringMVC、jquery的ajax、导出Excel文件
- 解决 Ajax 与导出文件下载冲突的心路历程
- poi+ajax之代码无误却无法导出文件
- ajax中使用Response.Redirect
- Ajax File Upload Response Handling
- ajax--返回值类型response
- SpringBoot学习-(九)SpringBoot中的AOP
- String,对象,Map,json互转封装类
- 数据结构—线性表—八进制数加法
- [ReactNative]ViewPager不显示图片
- cakes
- ajax response 导出文件
- 机器学习实践-k近邻算法-约会网站配对源码
- can't start git:usr/bin/git
- 通过函数给指针申请内存时遇到的内存泄漏问题
- js的slice,substring,substr,split用法
- 取double类型的几位有效数字
- 关于Toolbar的使用小结
- LeetCode231 Power of Two
- 【Leetcode】【python】First Missing Positive