<html>
<head>
<title>IE中图片转成base64</title>
<SCRIPT LANGUAGE="JavaScript">
var x= new ActiveXObject("Msxml2.XMLHTTP.6.0");
x.onreadystatechange=function(){
if(x.readyState<4) return;
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
var tmpNode = xml_dom.createElement("tmpNode");
tmpNode.dataType = "bin.base64";
tmpNode.nodeTypedValue = x.responseBody;
var base64str=tmpNode.text.replace(/\n/g,"");
document.getElementById('data1').value=base64str;
//document.write("<img src=\"data:image/jpeg;base64,"+base64str+"\" style='width:200;height:200'>")
}
function chgB64(){
//x.open("get","http://localhost:8090/lebts/images/testPicture2.jpg",true);
x.open("get","e:\\testPicture0.jpg",true);
x.send("");
}
</script>
</head>
<body>
<input type="button" onclick="chgB64();" value="TestB64" />
<textarea id="data1" rows=5 cols=60></textarea>
</body>
</html>
<head>
<title>IE中图片转成base64</title>
<SCRIPT LANGUAGE="JavaScript">
var x= new ActiveXObject("Msxml2.XMLHTTP.6.0");
x.onreadystatechange=function(){
if(x.readyState<4) return;
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
var tmpNode = xml_dom.createElement("tmpNode");
tmpNode.dataType = "bin.base64";
tmpNode.nodeTypedValue = x.responseBody;
var base64str=tmpNode.text.replace(/\n/g,"");
document.getElementById('data1').value=base64str;
//document.write("<img src=\"data:image/jpeg;base64,"+base64str+"\" style='width:200;height:200'>")
}
function chgB64(){
//x.open("get","http://localhost:8090/lebts/images/testPicture2.jpg",true);
x.open("get","e:\\testPicture0.jpg",true);
x.send("");
}
</script>
</head>
<body>
<input type="button" onclick="chgB64();" value="TestB64" />
<textarea id="data1" rows=5 cols=60></textarea>
</body>
</html>
本文介绍了一种使用IE浏览器将本地图片文件转换为Base64编码字符串的方法。通过JavaScript与XMLHttpRequest对象的结合应用,实现了图片读取并转换为Base64格式的功能。
1万+

被折叠的 条评论
为什么被折叠?



