如何使用JavaScript将图像转换为Base64字符串? [英] How can I convert an image into Base64 string using JavaScript?

查看:97
本文介绍了如何使用JavaScript将图像转换为Base64字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像转换为Base64字符串,以便将图像发送到服务器.

I need to convert my image to a Base64 string so that I can send my image to a server.

是否有任何JavaScript文件?否则,我该如何转换?

Is there any JavaScript file for this? Else, how can I convert it?

推荐答案

您可以使用HTML5 <canvas>:

You can use the HTML5 <canvas> for it:

创建画布,将图像加载到其中,然后使用 toDataURL() 进行操作获取Base64表示形式(实际上,这是一个data: URL,但是它包含Base64编码的图像).

Create a canvas, load your image into it and then use toDataURL() to get the Base64 representation (actually, it's a data: URL, but it contains the Base64-encoded image).

这篇关于如何使用JavaScript将图像转换为Base64字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆