浏览器下载/导出文件 及jQuery表单提交
1 比如以下按钮, 用于导出文件,如EXCEL文件。
<li><button class="whiteBg btn2" onclick="doExp(1);return false; "><i class="fa fa-save (alias) m-r-xs" ></i>导出所有工单</button></li><li><button class="whiteBg btn2" onclick="doExp(2);return false; ">- <i class="fa fa-file-text-o m-r-xs"></i>导出所选工单
</button></li>
function expExcel(){//alert("expExcel");var url="../user/expExcel?loginName=${user.loginName}&userName=${user.userName}&moblie=${user.mobile}";//alert(url);window.location.href=url;}- 或者用
- function exportExcel(){
if(flag){
flag = false;
window.location.href = "${ctx}/rpt/4gSite/empToExcel";setTimeout(function(){flag = true;},2000);
}
}- 或者, 都是一样的用法.
- $(function(){
exp=function(){
var query_time=$('#query_time').val(); window.location.href="../../doExp?query_time="+query_time;
}
});
@RequestMapping("/expToExcel")public void expToExcel(HttpServletRequest request, HttpServletResponse response) {UserContext uc = this.getUserContext(request);String loginName = request.getParameter("loginName");String userName= request.getParameter("userName");String moblie= request.getParameter("moblie");- User user=new User();
user.setLoginName(loginName);user.setUserName(userName);user.setMobile(moblie);List<User> users=this.userService.getListBy(user,uc);ExcelExportUtils2<User> exUser = new ExcelExportUtils2<User>();HSSFWorkbook workbook = exUser.exportExcel(new HSSFWorkbook(),"用户列表清单", users);ServletUtils.flushExcelOutputStream(request, response, workbook,"用户列表清单_"+DateUtil.formatDateToString("yyyyMMdd", new Date()));}
/*** 导出Excel,使用自定义的名字作为文件名* @param request* @param response* @param dataList* @throws UnsupportedEncodingException* @throws IOException*/public static void flushExcelOutputStream(HttpServletRequest request, HttpServletResponse response,HSSFWorkbook workbook,String fileName) {String userAgent = request.getHeader("User-Agent");String newFileName = null;try {fileName = URLEncoder.encode(fileName, "UTF8");} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}if (userAgent != null) {userAgent = userAgent.toLowerCase();// IE浏览器,只能采用URLEncoder编码if (userAgent.indexOf("msie") != -1) {newFileName = "filename=\"" + fileName + ".xls\"";}// Opera浏览器只能采用filename*else if (userAgent.indexOf("opera") != -1) {newFileName = "filename*=UTF-8''" + fileName +".xls";}// Safari浏览器,只能采用ISO编码的中文输出else if (userAgent.indexOf("safari") != -1) {try {newFileName = "filename=\""+ new String(fileName.getBytes("UTF-8"), "ISO8859-1")+ ".xls\"";} catch (UnsupportedEncodingException e) {e.printStackTrace();}}// Chrome浏览器,只能采用MimeUtility编码或ISO编码的中文输出else if (userAgent.indexOf("applewebkit") != -1) {try {fileName = MimeUtility.encodeText(fileName, "UTF8", "B");} catch (UnsupportedEncodingException e) {e.printStackTrace();}newFileName = "filename=\"" + fileName + ".xls\"";}// FireFox浏览器,可以使用MimeUtility或filename*或ISO编码的中文输出else if (userAgent.indexOf("mozilla") != -1) {newFileName = "filename*=UTF-8''" + fileName +".xls";}}//文件名编码结束。response.setHeader("Content-Disposition", "attachment;" + newFileName); // 这个很重要ServletUtils.setDisableCacheHeader(response);ServletOutputStream out = null;try {out = response.getOutputStream();workbook.write(out);out.flush();} catch (IOException e) {e.printStackTrace();}finally{if(out != null){try {out.close();} catch (IOException e) {logger.error(e.getMessage(), e);e.printStackTrace();}}}}
window.location.href = "../../../../download?"+$('#form_Report').serialize();window.location.href = "../../../../download?userName=${user.userName}"+"&"+$('#form_Report').serialize();

<input id="btnExport" class="btn btn-primary" type="button" value="导出"/>
$(document).ready(function() {// 表格排序tableSort({callBack : page});$("#btnExport").click(function(){top.$.jBox.confirm("确认要导出用户数据吗?","系统提示",function(v,h,f){if(v == "ok"){$("#searchForm").attr("action","${ctx}/sys/user/export").submit();}},{buttonsFocus:1});top.$('.jbox-body .jbox-icon').css('top','55px');});$("#btnImport").click(function(){$.jBox($("#importBox").html(), {title:"导入数据", buttons:{"关闭":true},bottomText:"导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!"});});});
DownLoadFile2({url:'../../../alarm/doExp',data:ids}); //ids为选中的数据的id如拼接字符串,如: 1,2,3,55,333,123,
//提交表单var DownLoadFile = function (options) {var config = $.extend(true, { method: 'post' }, options);var $iframe = $('<iframe id="down-file-iframe" />');var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');$form.attr('action', config.url);for (var key in config.data) {$form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');}$iframe.append($form);$(document.body).append($iframe);$form[0].submit();$iframe.remove();};//提交参数var DownLoadFile2 = function (options) {var config = $.extend(true, { method: 'post' }, options);var $iframe = $('<iframe id="down-file-iframe" />');var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');$form.attr('action', config.url);$form.append('<input type="hidden" name="ids" value="' + options.data + '" />');$iframe.append($form);$(document.body).append($iframe);$form[0].submit();$iframe.remove();};
var DownLoadFile = function (options) {var config ={ method: 'post' };var $form = $('<form method="' + config.method + '" />');$(document.body).append($form);$form.attr('action', options.url);for (var key in options.data) {$form.append('<input type="hidden" name="' + key + '" value="' + options.data[key] + '" />');}$form[0].submit();$form.remove();};
var DownLoadFile = function (options) {var config ={ method: 'post' };var $form = $('<form method="' + config.method + '" />');$(document.body).append($form);$form.attr('action', options.url);for (var key in options.data) {$form.append('<input type="hidden" name="' + key + '" value="' + options.data[key] + '" />');}$form[0].submit();$form.remove();};
- $(selector).serialize() 序列表表格内容为字符串,用于 Ajax 请求。可以对整个form,也可以只针对某部分。
$('#form').submit(function(event){event.preventDefault();$.ajax({url:' ',type:'post',data:$("form").serialize(),}
- $(selector).serializeArray()
serializeArray() 方法序列化表单元素(类似 .serialize() 方法),返回 JSON 数据结构数据。
注意:此方法返回的是 JSON 对象而非 JSON 字符串。需要使用插件或者第三方库进行字符串化操作。
返回的 JSON 对象是由一个对象数组组成的,其中每个对象包含一个或两个名值对 —— name 参数和 value 参数(如果 value 不为空的话)。举例来说:
[
{name: 'firstname', value: 'Hello'},
{name: 'lastname', value: 'World'},
{name: 'alias'}, // 值为空
]
.serializeArray() 方法使用了 W3C 关于 successful controls(有效控件) 的标准来检测哪些元素应当包括在内。特别说明,元素不能被禁用(禁用的元素不会被包括在内),并且元素应当有含有 name 属性。提交按钮的值也不会被序列化。文件选择元素的数据也不会被序列化。
该方法可以对已选择单独表单元素的对象进行操作,比如 <input>, <textarea>, 和 <select>。不过,更方便的方法是,直接选择 <form> 标签自身来进行序列化操作。
$("form").submit(function() {console.log($(this).serializeArray());return false;});上面的代码产生下面的数据结构(假设浏览器支持 console.log):[{name: avalue: 1},{name: bvalue: 2},{name: cvalue: 3},{name: dvalue: 4},{name: evalue: 5}]
- $.params() $.param()方法是serialize()方法的核心,用来对一个数组或对象按照key/value进行序列化。
序列化一个 key/value 对象:
var params = { width:1900, height:1200 };
var str = jQuery.param(params);
$("#results").text(str);结果:
width=1680&height=1050
浏览器下载/导出文件 及jQuery表单提交的更多相关文章
- Jquery表单提交后获取返回Json值
1.给form添加id值: <form action="/News/SaveMessage" method="post" accept-charset=& ...
- Easyui + jQuery表单提交 给 Controller patr1
2014-11-15 总结上周在公司开发所用到的技术,由于是刚找的工作(一个大三实习生)+自己的技术菜,有很多地方都是怎么想就怎么实现的, 如果你有什么更好的解决方法,在看见这篇博客的时候,希望你能 ...
- jquery表单提交获取数据(带toast dialog)
最近写了一个召集令,传统表单提交注册.写写遇到的费时间的点与解决办法 git项目地址:form-demo(针对于手机版,懒人可以直接使用,有排版和样式) demo使用Jquery,toast使用jqu ...
- jquery表单提交和重置
$('#myform').submit() 表单提交 $('#myform')[0].reset() 表单重置
- aspx中的表单验证 jquery.validate.js 的使用 以及 jquery.validate相关扩展验证(Jquery表单提交验证插件)
这一期我们先讲在aspx中使用 jquery.validate插件进行表单的验证, 关于MVC中使用 validate我们在下一期中再讲 上面是效果,下面来说使用步骤 jQuery.Valid ...
- jquery表单提交的新写法
$('form').submit()和$("form").submit() 这两种都可以实现form表单的提交 jquery中$('form').submit()和$(" ...
- Jquery 表单提交后3s禁用
<form action="${pageContext.servletContext.contextPath}/XXX/###" method="post" ...
- Jquery表单提交方式
1.使用调用submit方法 function tes1(){ //执行判断 if(校验通过){ $("#formId").submit(); }else{ return; } } ...
- jquery 表单提交不用action
1.今天我做完事去看了一下别人的代码,不用我们很常规的写法.我想让我们来学习一下吧! <form class="form-inline form-mess">//内容在 ...
随机推荐
- Django和Flask对于URL尾斜杠(back slash)的处理
最近在看Flask,其中提到了对于URL尾斜杠的处理.感觉算是一个需要注意的地方吧,就和Django的处理方式来进行一个简单的对比. 首先说下什么是尾斜杠. http://www.baidu.com/ ...
- Sliverlight 样式
UserControl 页面级样式UserControl.Resources style setter Property value. TargetType 应用的类型 使用 style static ...
- OJ-Triangle
这是Leet Code OJ上面的一道题,关于求从上到下的最小路径. 这是原题链接:https://leetcode.com/problems/triangle/ Given a triangle, ...
- C# 使用IEnumerable,yield 返回结果,同时使用foreach时,在循环内修改变量的值无效(二)
前言 在上篇文章中我得出结论,遍历迭代器修改迭代器中项目的值未生效,是因为使用了yield return,并且每次遍历迭代器都执行返回迭代器的方法.这篇文章是接着上篇文章,从代码实现的角度来验证出现这 ...
- 20145210 20145226 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
20145210 20145226 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 结对伙伴:20145226 夏艺华 实验报告封面 实验目的与要求 · 掌握在ARM开发板实现 ...
- AngularJS执行流程详解
一.启动阶段 大家应该都知道,当浏览器加载一个HTML页面时,它会将HMTL页面先解析成DOM树,然后逐个加载DOM树中的每一个元素节点.我们可以把AngularJS当做一个类似jQuery的js库, ...
- CentOS7 监控进程网络流量工具安装
服务器在做测试的时候,需要监控网络流量,用来了解在不同人数的时候服务器的网络使用量. 我们使用服务器环境是centos7,centos下通常使用iftop,或者nethogs来进行网络流量监控.这2个 ...
- 搞懂 SynchronizationContext
SynchronizationContext -MSDN 很让人失望 我不知道为什么,目前在.Net下关于这个类只有很少的资料.MSDN文档也只有很少的关于如何使用SynchronizationCon ...
- android摇一摇实现(仿微信)
这个demo模仿的是微信的摇一摇,是一个完整的demo,下载地址在最下面.下面是demo截图: 步驟: 1.手机摇动监听,首先要实现传感器接口SensorEventLi ...
- MySQL, 创建一个只读用户和一个所有权限用户
安装pasa需要配置mysql.基本知识学习一下 http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html MySQL 为关系型数据库 ...