|
各位同學小程序中碰到上傳圖片或者文件時有用到formData的
formData Object 否 HTTP 請求中其他額外的 form data
出現(xiàn)中文無法上傳或者出現(xiàn)亂碼時可以參考一下這篇跳坑指南微信小程序聯(lián)盟-uploadFile跳坑指南
里面講的很詳細了,這里歸納一下,解決方法:
在formData中對文字進行編碼,使用encodeURI()
wxml中例:
-
formData: {
-
name:encodeURI(name),
-
address:encodeURI(address),
-
}, // HTTP 請求中其他額外的 form data
上網(wǎng)搜了很多: UrlEncode編碼/UrlDecode解碼使用方法 UrlEncode編碼主要用于將字符串以URL編碼,返回一個字符串; 使用方法: 1、ASP中的用法:Server.URLEncode(“內(nèi)容”) 例如:
-
<%response.writeServer.UrlEncode("賴雪平網(wǎng)絡營銷博客")%>
2、PHP中的用法:urlencode(“內(nèi)容”) 例如:
-
<?echourlencode("賴雪平網(wǎng)絡營銷博客")?>
3、JSP中的用法:URLEncoder.encode(“內(nèi)容”) 例如:
-
<%java.net.URLEncoder.encode("賴雪平網(wǎng)絡營銷博客"); %>
UrlDecode解碼主要對字符串進行URL解碼,返回已解碼的字符串; 1、ASP中的用法:Server.UrlDecode(“內(nèi)容”) 例如:
-
<%response.writeServer.UrlDecode("%C0%B5%D1%A9%C6%BD%CD%F8%C2%E7%D3%AA%CF%FA%B2%A9%BF%CD")%>
2、PHP中的用法:urldecode(“內(nèi)容”) 例如:
-
<?echourldecode("%C0%B5%D1%A9%C6%BD%CD%F8%C2%E7%D3%AA%CF%FA%B2%A9%BF%CD")?>
3、JSP中的用法:URLDecoder.decode(“內(nèi)容”) 例如:
-
<%java.net.URLDecoder.decode("%C0%B5%D1%A9%C6%BD%CD%F8%C2%E7%D3%AA%CF%FA%B2%A9%BF%CD"); %>
發(fā)現(xiàn)沒有nodejs后臺的,還好皇天不負有心人,找到有用js寫的decodeurl函數(shù),給大家分享一下,有用的同學可以看一下。 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的一看就知道了,直接調(diào)用函數(shù)就行了。 唉!小程序坑真心多??!剛爬出來一個又掉入一個。 但是呢,從哪里掉進去就要再從哪里爬出來
|