小程序php返回中文乱码,小程序wx.uploadFile出现中文无法上传或者出现乱码

各位同学小程序中碰到上传图片或者文件时有用到formData的

formData Object 否 HTTP 请求中其他额外的 form data

出现中文无法上传或者出现乱码时可以参考一下这篇跳坑指南微信小程序联盟-uploadFile跳坑指南

里面讲的很详细了,这里归纳一下,解决方法:

在formData中对文字进行编码,使用encodeURI()

wxml中例:

formData: {

name:encodeURI(name),

address:encodeURI(address),

}, // HTTP 请求中其他额外的 form data

上网搜了很多:  UrlEncode编码/UrlDecode解码使用方法  UrlEncode编码主要用于将字符串以URL编码,返回一个字符串;  使用方法:  1、ASP中的用法:Server.URLEncode(“内容”) 例如:

2、PHP中的用法:urlencode(“内容”) 例如:

3、JSP中的用法:URLEncoder.encode(“内容”) 例如:

UrlDecode解码主要对字符串进行URL解码,返回已解码的字符串;  1、ASP中的用法:Server.UrlDecode(“内容”) 例如:

2、PHP中的用法:urldecode(“内容”) 例如:

3、JSP中的用法:URLDecoder.decode(“内容”) 例如:

发现没有nodejs后台的,还好皇天不负有心人,找到有用js写的decodeurl函数,给大家分享一下,有用的同学可以看一下。  js代码:

function urldecode (str) {

var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';

var replacer = function (search, replace, str) {

var tmp_arr = [];

tmp_arr = str.split(search);

return tmp_arr.join(replace);

};

// The hash_map is identical to the one in urlencode.

hash_map["'"] = ''';

hash_map['('] = '(';

hash_map[')'] = ')';

hash_map['*'] = '*';

hash_map['~'] = '~';

hash_map['!'] = '!';

hash_map[' '] = ' ';

hash_map['\u00DC'] = '�';

hash_map['\u00FC'] = '�';

hash_map['\u00C4'] = '�';

hash_map['\u00E4'] = '�';

hash_map['\u00D6'] = '�';

hash_map['\u00F6'] = '�';

hash_map['\u00DF'] = '�';

hash_map['\u20AC'] = '�';

hash_map['\u0081'] = '�';

hash_map['\u201A'] = '�';

hash_map['\u0192'] = '�';

hash_map['\u201E'] = '�';

hash_map['\u2026'] = '�';

hash_map['\u2020'] = '�';

hash_map['\u2021'] = '�';

hash_map['\u02C6'] = '�';

hash_map['\u2030'] = '�';

hash_map['\u0160'] = '�';

hash_map['\u2039'] = '�';

hash_map['\u0152'] = '�';

hash_map['\u008D'] = '�';

hash_map['\u017D'] = '�';

hash_map['\u008F'] = '�';

hash_map['\u0090'] = '�';

hash_map['\u2018'] = '�';

hash_map['\u2019'] = '�';

hash_map['\u201C'] = '�';

hash_map['\u201D'] = '�';

hash_map['\u2022'] = '�';

hash_map['\u2013'] = '�';

hash_map['\u2014'] = '�';

hash_map['\u02DC'] = '�';

hash_map['\u2122'] = '�';

hash_map['\u0161'] = '�';

hash_map['\u203A'] = '�';

hash_map['\u0153'] = '�';

hash_map['\u009D'] = '�';

hash_map['\u017E'] = '�';

hash_map['\u0178'] = '�';

hash_map['\u00C6'] = 'Æ';

hash_map['\u00D8'] = 'Ø';

hash_map['\u00C5'] = 'Å';

for (unicodeStr in hash_map) {

hexEscStr = hash_map[unicodeStr]; // Switch order when decoding

ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing

}

// End with d

ecodeURIComponent, which most resembles PHP's encoding functions

ret = decodeURIComponent(ret);

return ret;

}

至于怎么用,相信看得懂js的一看就知道了,直接调用函数就行了。  唉!小程序坑真心多啊!刚爬出来一个又掉入一个。  但是呢,从哪里掉进去就要再从哪里爬出来

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值