js代码生成form,解决mvc的url参数过长问题
在MVC项目中,通常下载的文件的简单方式是直接采用 location.href+查询参数方式。
var searchParams = {
studentName: $("#StudentName").val(),
birthDate: $("#BirthDate").val()
};
var baseurl = "@Url.Action("ExportData", "Student")";
location.href = baseurl + "?" + $.param(searchParams);
但是对于查询参数过长时url就会出错,因为IE浏览器对URL的最大限制为2083个字符,如果超过这个数字,提交按钮没有任何反应。对于Firefox浏览器URL的长度限制为65,536个字符,但当我测试时,最大只能处理8182个字符,这是因为url的长度除了浏览器限制外,还会受Web服务器的限制。注:可能有些朋友会想当然的认为,如果最大长度限制为2038字符,是不是参数差不多可以传递1000个左右的汉字。这样认为其实是不对的,对于中文的传 递,最终会为urlencode后的编码形式进行传递,如果浏览器的编码为UTF8的话,一个汉字最终编码后的字符长度为9个字符。(此段话引用URL最大长度问题,非常感谢)
所有楼主改用form提交,由于系统有多个页面存在导出文件功能,并且一个页面也存在导出存在多个Excel的情况,故写了个插件,有啥意见和建议欢迎批评改正。
原始的方式:提前在界面先定义好form,当用户点击时提及。
//原生写法
<form method="POST" action="@Url.Action("ExportData", "Student")" id="exportForm">
<input type="hidden" id="studentName" name="studentName" />
<input type="hidden" id="birthDate" name="birthDate" />
</form>
//MVC封装好的方法
@Html.BeginForm("ExportData", "Student", FormMethod.Post, new { id = "exportForm" }) {
<input type="hidden" id="stationCodes" name="stationCodes" />
<input type="hidden" id="studentName" name="studentName" />
<input type="hidden" id="birthDate" name="birthDate" />
}
封装好的插件: var
/* 导出excel
11.13.2014 - LQZ - 全选站点url过长ie超过2083个字符将报错,将所有url整合成form提交
*/
;(function() {
var instance;
window.GridExportOperate = function () {
if (instance) {
return instance;
}
instance = this;
this.gridexport = function(formPara, formId, formUrl) {
if (!$("#" + formId).length) {
var form = document.createElement("form");
form.type = "post";
//form.style.display = "none";
document.body.appendChild(form);
form.action = formUrl;
for (item in formPara)
createInput(form, "hidden", item, formPara[item]);
form.submit();
} else {
for (item in formPara)
changeInput(item, formPara[item]);
$("#" + formId).submit();
}
}; function createInput(form, type, name, value) {
var tmpInput = document.createElement("input");
tmpInput.type = type;
tmpInput.name = name;
tmpInput.value = value;
form.appendChild(tmpInput);
}
function changeInput(name, value) {
$('#' + name).val(value);
}
};
})(jQuery);
调用方法创建类的实例后调用。
var gridxportOperate = new GridExportOperate(); gridxportOperate.gridexport(formPara, formId, baseurl);
js代码生成form,解决mvc的url参数过长问题的更多相关文章
- 分享一个好用的函数吧,将js中的对象转成url参数
JavaScript&jQuery获取url参数方法 这个函数呢是自己在写基于Vue+ElementUI管理后台时用到的,,下面列出来两种使用方式: 最普通的,封装一个js函数 /** * 对 ...
- 解决window.location.href参数太长
前言:一提到页面跳转,最常用的一般就是window.location.href,如果需要带参数,也许可以在后面用?拼上,但这样并不安全,而且有个更严重的问题,这样的拼接是有长度限制的,如果达到好几千个 ...
- 解决window.location.href参数太长 post提交数据
前言:一提到页面跳转,最常用的一般就是window.location.href,如果需要带参数,也许可以在后面用?拼上,但这样并不安全,而且有个更严重的问题,这样的拼接是有长度限制的,如果达到好几千个 ...
- 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数
目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...
- 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数,以及如何根据URL解析出匹配到MVC路由的Controller和Action
目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...
- js的form表单提交url传参数(包含+等特殊字符)的解决方法
方法一:(伪装form表单提交) linkredwin = function(A,B,C,D,E,F,G){ var formredwin = document.createElemen ...
- js获取或设置当前窗口url参数
直接上代码 // 获取当前窗口url中param参数的值 function get_param(param){ var query = location.search.substring(1).spl ...
- JS页面跳转加密解密URL参数
页面跳转加密参数 window.location.href="foot.html?"+btoa(encodeURIComponent("goodid="+goo ...
- JS request函数 用来获取url参数
function request(strParame) { var args = new Object( ); var query = location.search.substring(1); va ...
随机推荐
- CvMat结构
一.创建矩阵的方式: 1.cvCreateMat(int rows,int cols,int type),Type可以使任何预定义类型.Type的写法规则:CV_<bit_depth>(S ...
- 细说php一些常见的知识点
一.认识脚本语言 1.常见的脚本语言有:html,css,js,asp,Python等 2.脚本语言的特性: a.语法和机构通常比较简单 b.学习和使用通常比较简单 c.通常以容易修改程序的“解释”作 ...
- TOJ2647
2647: How Many Tables 时间限制(普通/Java):1000MS/ ...
- Python循环语句
1.Python循环类型 1.while循环:在某条件下,循环执行某段程序 a. while语句有两个重要命令:continue,break来跳出循环. continue用来跳出该次循环 break用 ...
- cocos2dx 3.x tolua 分析
cocos2dx 3.x 版本已经出到3.10了,终于决定要进行引擎版本升级,c++配合lua进行游戏开发,从3.x版本开始cocos使用了新的tolua方式,由于不懂python,折腾tolua搞的 ...
- VS2008编译bat
工程文件为AirCode,批处理文件为bulit.bat(与*.sln文件在同级目录). 以下是批处理的代码: echo %~dp0 rem set build_config="Debug| ...
- Http中 Post和 Get的区别
1.表面上的区别 1.GET在浏览器回退时,是无害的,而Post会再次提交请求 2.Get产生的Url地址会被Bookmark,而Post不会 3.Get请求会被浏览器主动Cache,而Post不会, ...
- (转) 变分自编码器(Variational Autoencoder, VAE)通俗教程
变分自编码器(Variational Autoencoder, VAE)通俗教程 转载自: http://www.dengfanxin.cn/?p=334&sukey=72885186ae5c ...
- php区分new static 和new self
关键点在于一个是静态绑定,一个是延迟绑定 <?php class A{ public function __construct() { } public function createObjSt ...
- case when
SELECT * FROM category WHERE EXISTS (SELECT * FROM goods WHERE goods.cat_id = category.cat_id) cat ...