📜  URL.createObjectURl 与 fileReader 有何不同 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:52.593000             🧑  作者: Mango

代码示例1
const fileSelect = document.getElementById("fileSelect"),
    fileElem = document.getElementById("fileElem"),
    fileList = document.getElementById("fileList");

fileSelect.addEventListener("click", function (e) {
  if (fileElem) {
    fileElem.click();
  }
  e.preventDefault(); // prevent navigation to "#"
}, false);

fileElem.addEventListener("change", handleFiles, false); 

function handleFiles() {
  if (!this.files.length) {
    fileList.innerHTML = "

No files selected!

"; } else { fileList.innerHTML = ""; const list = document.createElement("ul"); fileList.appendChild(list); for (let i = 0; i < this.files.length; i++) { const li = document.createElement("li"); list.appendChild(li); const img = document.createElement("img"); img.src = URL.createObjectURL(this.files[i]); img.height = 60; img.onload = function() { URL.revokeObjectURL(this.src); } li.appendChild(img); const info = document.createElement("span"); info.innerHTML = this.files[i].name + ": " + this.files[i].size + " bytes"; li.appendChild(info); } } }