简体   繁体   English

将图像转换为base64

[英]Convert image to base64

I'm trying to convert an image into base64 using javascript, however if I request the URL with jquery, and encode with javascript, the results are different than what I receive from here (http://www.scalora.org/projects/uriencoder/) when saving and uploading the image.. 我正在尝试使用javascript将图像转换为base64,但是,如果我使用jquery请求URL并使用javascript进行编码,则结果与我从此处收到的结果不同(http://www.scalora.org/projects/ uriencoder /)在保存和上传图像时。

Is there any reason for this? 有什么理由吗?

I'm just using $.get and base64 encoding function data() 我只是在使用$ .get和base64编码函数data()

Another SO Question covers this exact topic and has a solution that works for IE and Firefox. 另一个SO问题涵盖了这一确切主题,并提供了适用于IE和Firefox的解决方案。 The short answer is that XHR really isn't designed for binary data and the data gets corrupted or truncated when converted to a string for XHR.responseText . 简短的答案是XHR实际上不是为二进制数据设计的,并且当转换为XHR.responseText的字符串时,数据会被破坏或截断。

It would be best to Base 64 encode on the server side. 最好在服务器端对Base 64进行编码。

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

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