DOMString-URLまたはbase 64は、画像を展示します.
DOMString-URLまたはbase 64は、画像を展示します.
直接コード
直接コード
OSS
, ~
window.onload = function(){
let inputElement = document.getElementById("file");
let dropbox = document.getElementById("box");
inputElement.addEventListener("change", handleFiles , false);
function handleFiles(fileList, isbox = true) {
if(isbox){
fileList = fileList.target.files;
}
console.log(fileList);
for (let i = 0; i < fileList.length; i++) {
let img = document.createElement("img"); //let
dropbox.appendChild(img);
/*img.src = window.URL.createObjectURL(fileList[i]); //DOMstring-URL
img.onload = function(){
window.URL.revokeObjectURL(this.src);
}*/
var reader = new FileReader();
reader.onload = function(e){
img.src = e.target.result; //base64
}
reader.readAsDataURL(fileList[i]);
}
}
dropbox.addEventListener("dragenter",function(e){
e.stopPropagation();
e.preventDefault();
},false);
dropbox.addEventListener("dragover", function(e){
e.stopPropagation();
e.preventDefault();
}, false);
dropbox.addEventListener("drop", function(e){
e.stopPropagation();
e.preventDefault();
var dt = e.dataTransfer;
var files = dt.files;
handleFiles(files,false);
}, false);
}