jquery ajax post 提交的时候,在后台request.QueryString 获取得到的却得到了是十六进制的乱码用escape() 来编码字符串能解决这个问题。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Jquery/jquery-1.3.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
alert(1)
$("#sub").click(function () {
alert(0);
var filePath = $("#typeFile").val();
alert(filePath)
alert("escape" + escape(filePath))
alert("unescape"+unescape(filePath))
$.post("Default.aspx?a="+unescape(filePath)+"&path=" +escape(filePath), function (data) {
alert(data);
}, "text");
});
});
</script>
</head>
<body>
<input type="file" name="typeFile" id="typeFile" />
<input type="button" id="sub" value="提 交" />
</body>
</html>
后台:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string path = Request.QueryString["path"];
if (!string.IsNullOrEmpty(path))
{
string type= Request.QueryString["a"];
FileStream stream = File.OpenRead(path);
byte[] by = new byte[stream.Length];
stream.Read(by, 0, Convert.ToInt32(stream.Length));
string newFileName = DateTime.Now.Ticks + path.Substring(path.LastIndexOf('.'));
string savePath = Server.MapPath("UpFiles//"+newFileName);
File.WriteAllBytes(savePath, by);
Response.Write("图片上传成功!");
Response.End();
}
}
注:
定义和用法
unescape() 函数可对通过 escape() 编码的字符串进行解码。
语法
unescape(string)
参数 | 描述 |
---|---|
string | 必需。要解码或反转义的字符串。 |
返回值
string 被解码后的一个副本。
说明
该函数的工作原理是这样的:通过找到形式为 %xx 和 %uxxxx 的字符序列(x 表示十六进制的数字),用 Unicode 字符 /u00xx 和 /uxxxx 替换这样的字符序列进行解码。
定义和用法
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。
语法
escape(string)
参数 | 描述 |
---|---|
string | 必需。要被转义或编码的字符串。 |
返回值
已编码的 string 的副本。其中某些字符被替换成了十六进制的转义序列。
说明
该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( ) 。其他所有的字符都会被转义序列替换。
提示和注释
提示:可以使用 unescape() 对 escape() 编码的字符串进行解码。
注释:ECMAScript v3 反对使用该方法,应用使用 decodeURI() 和 decodeURIComponent() 替代它。