Jquery ajax Post 提交 乱码解决方案

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() 替代它。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值