按照变量和值相对应的方式,传递到ACTION所指向URL,POST可以传递大数据量的信息到服务器,通常文件上传就是使用POST方式上传。
为解决GET方式传递大数据量参数的问题,要使用POST方式进行数据提交,下面是一个用来代替window.open的方法
1.JavaScript
/*
* PostNewWin
* Author:ppchen
*/
var
PostNewWin
=
function
(url){
var
urlArr
=
url.split(
"
?
"
);
var
postUrl
=
urlArr[
0
];
var
postData
=
urlArr[
1
];
var
iframe
=
document.getElementById(
"
postData_iframe
"
);
if
(
!
iframe){
iframe
=
document.createElement(
"
iframe
"
);
iframe.id
=
"
postData_iframe
"
;
iframe.scr
=
"
about:blank
"
;
iframe.frameborder
=
"
0
"
;
iframe.style.width
=
"
0px
"
;
iframe.style.height
=
"
0px
"
;
var
form
=
document.createElement(
"
form
"
);
form.id
=
"
postData_form
"
;
form.method
=
"
post
"
;
form.target
=
"
_blank
"
;
document.body.appendChild(iframe);
iframe.contentWindow.document.write(
"
<body>
"
+
form.outerHTML
+
"
</body>
"
);
}
iframe.contentWindow.document.getElementById(
"
postData_form
"
).innerHTML
=
"
<input name=‘postData‘ id=‘postData‘ type=‘text‘ value=‘
"
+
postData
+
"
‘/>
"
;
iframe.contentWindow.document.getElementById(
"
postData_form
"
).action
=
postUrl;
iframe.contentWindow.document.getElementById(
"
postData_form
"
).submit();
};
2.CSharp
///
<summary>
///
从Form中取得参数
///
Author:ppchen
///
</summary>
///
<returns>
参数集合
</returns>
private
NameValueCollection ParseFormData()
{
NameValueCollection sQueryString
=
new
NameValueCollection();
if
(
this
.Request.Form.Count
>
0
&&
this
.Request.Form[
"
postData
"
]
!=
null
)
{
string
sPostData
=
this
.Request.Form[
"
postData
"
].ToString();
sPostData
=
sPostData.Trim(
new
char
[] {
‘
&
‘
,
‘
‘
});
if
(
!
string
.IsNullOrEmpty(sPostData))
{
string
[] sParameterList
=
sPostData.Split(
‘
&
‘
);
for
(
int
i
=
0
; i
<
sParameterList.Length; i
++
)
{
string
[] sParameter
=
sParameterList[i].Split(
‘
=
‘
);
for
(
int
j
=
0
; j
<
sParameter.Length; j
=
j
+
2
)
{
sQueryString.Add(sParameter[j], HttpUtility.UrlDecode(sParameter[j
+
1
]));
}
}
}
}
return
sQueryString;
}
通过以上的JS代码在客户端打开页面,通过以上的CS代码在服务端取得参数,这样使用了POST方式解决了GET方式中URL的长度限制,可以传递大数据量的参数了:)