我找到了这个转换器https://github.com/rujs/Image-to-Base64-Converter/blob/master/index.html 它完全没问题但它在Firefox中不起作用。 谁能告诉我为什么?
这是代码:
JS
var reader = new FileReader(),
rImg = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i,
file;
reader.onload = function (e) {
document.getElementById('preview').src = e.target.result;
document.getElementById('base64').innerText = e.target.result;
};
function convert() {
file = document.getElementById("upload").files[0];
if (!rImg.test(file.type)) {
alert("You must select a valid image file!");
return;
}
console.log(file.type + ' :: ' + file.name + ' :: ' + file.size)
reader.readAsDataURL(file);
}
HTML
<h4>Image to Base64 converter</h4>
<img id="preview" style="width: 250px; height: 250px;" src="" alt="Image preview" />
<input id="upload" type="file" onchange="convert();" style=""/>
<h4>Results</h4>
<textarea id="base64" style="width: 100%; height: 200px;" ></textarea>
答案 0 :(得分:0)
需要更改
document.getElementById(&#39; base64&#39;)。innerText = e.target.result;
到
document.getElementById(&#39; base64&#39;)。innerHTML = e.target.result;