|
以前,也经常做base64转图片,从来没出现过什么问题,
今天,我前台用js把Canvas转成的Base64位字符串
bbb是我们得到的字符串,然后我用ajax传到的后台,如下;
后台接收代码:
我靠,竟然第二句报错了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
错误列表
{"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. "} {"Invalid length for a Base-64 char array or string."}
第一个错误是,因为,字符串中带有data:image/jpeg;base64这玩意,把这玩意去掉就好了!
这段代码解决第一个错误!!!
第二个错误,不知道 因为啥错的,搞了一个小时,最后从老外论坛看到了几句话
The length of a base64 encoded string is always a multiple of 4. If it is not a multiple of 4, then = characters are appended until it is. A query string of the form ?name=value has problems when the value contains = charaters (some of them will be dropped, I don't recall the exact behavior). You may be able to get away with appending the right number of = characters before doing the base64 decode.
翻译后如下,大体看看就行了
一个Base64编码的字符串的长度是4的倍数。如果不是4的倍数,然后=字符追加直到。一个表格的查询字符串?name=value有问题的时候价值包含=特征(他们中的一些会下降,我不记得确切的行为)。你可以去添加正确的数量=人物之前做base64解码。
最后,加了几句代码,就搞定了,完整代码如下:
|
上一篇:推荐一款js图片裁切插件下一篇:Microsoft.Office.Interop.Word文档动态生成表格
|