各位同学小程序中碰到上传图片或者文件时有用到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(“内容”) 例如:
-
<%response.writeServer.UrlEncode("赖雪平网络营销博客")%>
2、PHP中的用法:urlencode(“内容”) 例如:
-
<?echourlencode("赖雪平网络营销博客")?>
3、JSP中的用法:URLEncoder.encode(“内容”) 例如:
-
<%java.net.URLEncoder.encode("赖雪平网络营销博客"); %>
UrlDecode解码主要对字符串进行URL解码,返回已解码的字符串; 1、ASP中的用法:Server.UrlDecode(“内容”) 例如:
-
<%response.writeServer.UrlDecode("%C0%B5%D1%A9%C6%BD%CD%F8%C2%E7%D3%AA%CF%FA%B2%A9%BF%CD")%>
2、PHP中的用法:urldecode(“内容”) 例如:
-
<?echourldecode("%C0%B5%D1%A9%C6%BD%CD%F8%C2%E7%D3%AA%CF%FA%B2%A9%BF%CD")?>
3、JSP中的用法:URLDecoder.decode(“内容”) 例如:
-
<%java.net.URLDecoder.decode("%C0%B5%D1%A9%C6%BD%CD%F8%C2%E7%D3%AA%CF%FA%B2%A9%BF%CD"); %>
发现没有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的一看就知道了,直接调用函数就行了。 唉!小程序坑真心多啊!刚爬出来一个又掉入一个。 但是呢,从哪里掉进去就要再从哪里爬出来
|