如何获取上传图像的图像名称和大小



我使用这段代码使我的东西工作。通过使用下面的代码,我试图得到图像的名称和它的大小在字节/Mbps。

'use strict';
const myImages = [];
function addImage(imageBlob) {
myImages.push(imageBlob);
}
function redrawImages() {
const divForImages = document.getElementById('myImages');
divForImages.innerHTML = '';
myImages.forEach((imageBlob) => {
const img = document.createElement('img');
img.src = URL.createObjectURL(imageBlob);
divForImages.appendChild(img);
});
}
function addImageAndRedraw() {
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 1) {
addImage(fileInput.files[0]);
redrawImages();
} else {
alert('No file selected. Select a file and try again.');
}
}
const button = document.getElementById('button');
button.addEventListener('click', addImageAndRedraw);
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My images</h1>
<input id="fileInput" type="file" accept="image/*" multiple="false" value="Select image">
<input id="button" type="button" value="Add image and redraw">   
<hr>
<div id="myImages"></div>
<script src="album.js"></script>
</body>
</html>

我尝试使用img.name,但它已被弃用。

怎么做?非常感谢您的帮助

imageBlob不是blob。它是File,它有name的性质。名称不会被转移到imgDOMElement。

最新更新