我正在尝试在base64字符串中获取图像的内容。
最初我是用readAsDataURL
但because I want to validate the mimetype
on the client side来做的,似乎我必须使用readAsArrayBuffer,同时指出on this site。
所以我以前工作得很好:
var reader = new FileReader();
reader.onloadend = function(event) {
var base64 = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
现在我添加了mimetype验证,我有以下内容:
var reader = new FileReader();
reader.onloadend = function(event) {
var realMimeType = getRealMimeType(reader);
if (realMimeType !== 'unknown') {
var emptyBufferArray = reader.result; //nothing
}else{
alert("Invalid mime type!");
}
};
reader.readAsArrayBuffer(event.target.files[0]); //<-- notice the difference
Reproduction online(未获取base64字符串)
答案 0 :(得分:6)
我发现这样做的唯一方法是使用两个不同的FileReader
实例,一个在另一个内。
的Javascript
$(document).on('change', '#upload', addBackgroundImage);
function addBackgroundImage(event) {
var reader = new FileReader();
var readerBase64 = new FileReader();
var image = event.target.files[0];
reader.onloadend = function() {
var realMimeType = getRealMimeType(reader);
if (realMimeType !== 'unknown') {
readerBase64.readAsDataURL(image);
} else {
alert("Please upload a valid image file");
}
};
reader.readAsArrayBuffer(image);
readerBase64.onloadend = function(){
var base64 = this.result;
$('.bg').css('background-image', 'url('+base64+')');
};
$('#upload').val('');
}
function getRealMimeType(reader){
var arr = (new Uint8Array(reader.result)).subarray(0, 4);
var header = '';
var realMimeType;
for (var i = 0; i < arr.length; i++) {
header += arr[i].toString(16);
}
// magic numbers: http://www.garykessler.net/library/file_sigs.html
switch (header) {
case "89504e47":
realMimeType = "image/png";
break;
case "47494638":
realMimeType = "image/gif";
break;
case "ffd8ffDB":
case "ffd8ffe0":
case "ffd8ffe1":
case "ffd8ffe2":
case "ffd8ffe3":
case "ffd8ffe8":
realMimeType = "image/jpeg";
break;
default:
realMimeType = "unknown"; // Or you can use the blob.type as fallback
break;
}
return realMimeType;
}
HTML
<input type="file" id="upload" />
<div class="bg"></div>
答案 1 :(得分:3)
我还没有设法使用base64
,但我找到了使用blob
的方法。
如果您找到使用base64
的方法,请添加您的答案。
现在我的图片字符串看起来像这样,我相信这会给我带来一些问题:
background-image: url("blob:https%3A//fiddle.jshell.net/214b3c01-5b38-4aae-b839-e35cf57a5190");
我从一个关于how to render a retrieved image as a blob URL
的小提琴中得到了提示然后我只用a little improvement将它应用到我的代码中,然后瞧!!
Reproduction online 在IE中工作&gt; 9,Chrome,Firefox ......
整个代码:
HTML
<input type="file" id="upload" />
<div class="bg"></div>
的Javascript
$(document).on('change', '#upload', addBackgroundImage);
function addBackgroundImage(event) {
var reader = new FileReader();
reader.onloadend = function(event) {
var realMimeType = getRealMimeType(reader);
if (realMimeType !== 'unknown') {
var base64 = reader.result;
var arrayBufferView = new Uint8Array( this.result );
var blob = new Blob( [ arrayBufferView ], { type: realMimeType } );
var urlCreator = window.URL || window.webkitURL || {}.createObjectURL;
var imageUrl = urlCreator.createObjectURL( blob );
$('.bg').css('background-image', 'url('+imageUrl+')');
} else {
alert("Please upload a valid image file");
}
}
reader.readAsArrayBuffer(event.target.files[0]);
$('#upload').val('');
}
function getRealMimeType(reader){
var arr = (new Uint8Array(reader.result)).subarray(0, 4);
var header = '';
var realMimeType;
for (var i = 0; i < arr.length; i++) {
header += arr[i].toString(16);
}
// magic numbers: http://www.garykessler.net/library/file_sigs.html
switch (header) {
case "89504e47":
realMimeType = "image/png";
break;
case "47494638":
realMimeType = "image/gif";
break;
case "ffd8ffDB":
case "ffd8ffe0":
case "ffd8ffe1":
case "ffd8ffe2":
case "ffd8ffe3":
case "ffd8ffe8":
realMimeType = "image/jpeg";
break;
default:
realMimeType = "unknown"; // Or you can use the blob.type as fallback
break;
}
return realMimeType;
}
答案 2 :(得分:2)
您可以尝试此"stolen"功能:
function arrayBufferToBase64(buffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
答案 3 :(得分:1)
下面的解决方案仅使用一个FileReader为我工作:
reader.onloadend = function(event) {
const arrayBuffer = reader.result;
const blob = new Blob([arrayBuffer], {type: 'image/png'});
let src = URL.createObjectURL(blob);
};
reader.readAsArrayBuffer(event.target.files[0]);
希望有所帮助!