Image convert to Base64(图片转Base64)
问题描述
<input type="file" id="asd"/>
一旦用户选择(在提交表单之前),我想在 base64 中获取图像
I would like to get the image in base64 once the user chose that (before submitting the form)
类似:
$(input).on('change',function(){
var data = $(this).val().base64file(); // it is not a plugin is just an example
alert(data);
});
我阅读了有关 File API 和其他内容的信息,我想要一个简单的跨浏览器解决方案(IE6/IE7 显然不包括在内)
I read about File API and other stuffs, I would like a simple and cross-browsers solution (IE6/IE7 excluded obviously)
任何帮助表示感谢.
推荐答案
function readFile() {
if (this.files && this.files[0]) {
var FR= new FileReader();
FR.addEventListener("load", function(e) {
document.getElementById("img").src = e.target.result;
document.getElementById("b64").innerHTML = e.target.result;
});
FR.readAsDataURL( this.files[0] );
}
}
document.getElementById("inp").addEventListener("change", readFile);
<input id="inp" type='file'>
<p id="b64"></p>
<img id="img" height="150">
(P.S:base64 编码的图像(字符串)是原始图像数据大小的 4/3)
(P.S: A base64 encoded image (String) 4/3 the size of the original image data)
检查这个答案多张图片上传.
浏览器支持:http://caniuse.com/#search=file%20api
更多信息:https://developer.mozilla.org/en-US/docs/Web/API/FileReader
这篇关于图片转Base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:图片转Base64
基础教程推荐
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bokeh Div文本对齐 2022-01-01
