var c = document.createElement/'canvas'/;
var img = document.getElementById/'Img1'/;
c.height = img.naturalHeight;
c.width = img.naturalWidth;
var ctx = c.getContext/'2d'/;
ctx.drawImage/img, 0, 0, c.width, c.height/;
var base64String = c.toDataURL//;
// Author James Harrington 2014
function base64/file, callback/{
var coolFile = {};
function readerOnload/e/{
var base64 = btoa/e.target.result/;
coolFile.base64 = base64;
callback/coolFile/
};
var reader = new FileReader//;
reader.onload = readerOnload;
getBase64/fileObjectFromInput, function/base64Data/{
console.log/"Base64 of file is", base64Data/; // Here you can have your code which uses Base64 for its operation, // file to Base64 by oneshubh
}/;
13 个回复
快网
赞同来自:
1. 一种方法: FileReader
以大二进制对象的形式加载图像
https://developer.mozilla.org/ ... quest
和使用
https://developer.mozilla.org/ ... eader
转换为
https://developer.mozilla.org/ ... _URIs
:
此代码代码也可以使用函数实现。 WHATWG
https://developer.mozilla.org/ ... Fetch
:
列出的方法
:
缺乏浏览器支持
为了更好的压缩
适用于其他文件类型
浏览器支持
:
http://caniuse.com/#feat=filereader
http://caniuse.com/#feat=fetch
2. 方法:帆布
将图像下载到图像对象中,将其绘制在一个未填写的画布上并将画布转换回 dataURL.
https://gist.github.com/HaNdTriX/7704632
支持的输入格式
:
,
,
,
,
,
,
,
,
,
支持的输出格式
:
,
,
/铬/
浏览器支持
:
http://caniuse.com/#feat=canvas
Internet Explorer 10 /Internet Explorer 10 只需使用相同的源图像/
3. 方法:来自本地文件系统的图像
如果要从用户文件系统转换图像,则需要使用其他方法。
使用
https://developer.mozilla.org/ ... eader
:
董宝中
赞同来自:
:
创建画布,将图像下载到其中,然后使用
https://developer.mozilla.org/ ... ement
, 得到一个想法 Base64 /实际上它
URL, 但它包含一个编码图像 Base64/.
</canvas>
卫东
赞同来自:
帅驴
赞同来自:
然后,您可以将其转换如下。
石油百科
赞同来自:
你如何使用它
裸奔
赞同来自:
.
示范:
https://codverter.com/src/webe ... 97b68
石油百科
赞同来自:
使用示例:
郭文康
赞同来自:
http://www.w3.org/TR/FileAPI/
, 但实际上不受支持。
二哥
赞同来自:
https://coderoad.ru/26212792/
涵秋
赞同来自:
要执行文件下载事件,请调用此功能:
存储数据 Base64 在隐藏文件中使用。
知食
赞同来自:
董宝中
赞同来自:
现在在事件处理程序中使用它。
涵秋
赞同来自:
https://en.wikipedia.org/wiki/Dojo_Toolkit
Dojo , 这为我们提供了一种直接的编码或解码方式 Base64.
试试吧:
用于编码数组字节使用 dojox.encoding.base64:
用于在编码中解码字符串 Base64: