简体   繁体   English

在浏览器中将base64编码的字符串显示为图像时出错?

[英]Error in displaying base64 encoded string as image in browser?

I am working on a website for a friend which takes in a signature from a wacom STU-300 signature tablet, to then be placed in the database. 我正在一个朋友的网站上工作,该朋友从wacom STU-300签名板中获取签名,然后将其放置在数据库中。 (as any data type as long it is readable, I tried BLOB and varchar ). (因为任何可读的数据类型,我都尝试了BLOB and varchar )。 After checking the documentation I found out that there is a method where you can retrieve the data as a base64 encoded string after taking the signature. 查看文档后,我发现有一种方法可以在获取签名后以base64 encoded字符串的形式检索数据。 This is an example I printed from a signature after extracting the base64 encoded string: 这是我提取base64 encoded字符串后从签名打印的示例:

"RlP5QhsBHAECGUVDFxYZVCQFBwkDBggLBA0MFB0cGhsYFTgCIgUgJx3EG8LuM6ZpqwR8ScEztVwTqbxuB8+gFfRUzHv7lXdFA46EAUMBARcEA1dITxYCASAZIgQgcKIShjL9FJx63Xpnkli3HoFMatdpMwfX7Bg528NKz2JUAgE0JAEBBQoBBQCsTaD5BQIABwoBBQDEE/mABgIBCQgBBQAAkE4EAAMJAmABCAIDXQAQBmcCYOIfBghgkBgoNERQYGhsdISAmJyktLCsoJSMeHBMMAvrz7egHl/5+gHs97/Dy8wvQP128/Lv6+jk3tzX1NEHQ+09jcB3/eDb2tcHVo2OHr+QgYKj5QX212fH17cGZURTYvHx4V/ACGoCYMMHCAhj/QkNEBIBF/QZFBMNBwP9+PPs497WAdO90tPX2drb2Nvf5u/4AAkSHiYxNz5FTVRXVVFQTkxKRkRHRkVCPzsxJRMF+Onc0MjBvLe5vcDCv73BydXl9QQTHicwNDcBNv01Nz49CwfIAQABAQEABAkCYAEIAQNdAEANYAJgYQUIWqA4UGh4kIiAYEgwKCAYgIAGICCA/AgMIEIB8BgoQEH4CgQCAAJP8AAH/yAf4EBgIAA/4gIH6H879gAKBhAgfof73+AgYQTwGCgggUIFv//Pr38/IPf3j0rWYAwIAQUA/wMAAAAUCAEEAKxNAMQTHQ8OBjAQs9uhtgasnhcBsPYcKypNaWNyb3NvZnQ7J1dpbmRvd3MgOCsnOyh1bmtub3duKTs7Ni4zLjk2MDAaFhVTVFU7J1NUVS0zMDAnOzEuMC44OzAbCQgwMDU3O1NUVRgHjJfKqQXgARUKAQTqA+k7tw23DTgBBA=="

Edit: Thanks vjdhama for removing the spaces now it does come up as a valid base64 string however I am still not able to convert this back into an image file. 编辑:感谢vjdhama现在删除空格,它确实作为有效的base64字符串出现,但是我仍然无法将其转换回图像文件。 When I tried decoding this back into an image using the base64 decoder at WUtils, it comes up as 当我尝试使用WUtils的base64解码器将其解码回图像时,它显示为

FS�BECT$    
8" '���3�i�|I�3�\��nϠ�T�{��wE��CWHO " p��2��z�zg�X��Lj�i3��9��J�bT4$

instead of an image. 而不是图像。 I need to display the base64 encoded string in image form in the browser with php . 我需要使用php在浏览器中以图像形式显示base64编码的字符串。

To check for validity of string we could use this regex 要检查字符串的有效性,我们可以使用此正则表达式

var base64 = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})([=]{1,2})?$");

Just test your string by 只是测试你的字符串

base64.test(str);

You can read more here : RegEx to parse or validate Base64 data 您可以在此处了解更多信息: RegEx可以解析或验证Base64数据

UPDATE : 更新

You can also decode data using window.atob(str) web api method. 您也可以使用window.atob(str)网络api方法解码数据。

You can read more it here : https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/atob 您可以在这里阅读更多内容: https : //developer.mozilla.org/en-US/docs/Web/API/WindowBase64/atob

Refer this for more on base64 encoding and decoding : https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding 有关更多有关base64编码和解码的信息,请参考: https : //developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM