Cameron McCormack · @heycam
<input type=file>HTMLInputElement has a .files property, a FileList<input>.files is a File object, providing access to information about the filechange event is dispatched to the <input> when
files are selectedSelect a file...
<input type=file onchange="showFilePreview(event.target)">
<pre>Select a file...</pre>
<script>
function showFilePreview(input) {
document.querySelector("pre").textContent =
input.files[0].name + ": " +
input.files[0].size + " bytes";
}
</script>
.name – just the filename part, not the path.size – file size in bytes.lastModifiedDate – as a Date object.type – a media type like “text/plain” or “image/jpeg”FileReader object – asyncload event listener to it for when it is doneFileReader passing in the FileSelect a file...
<input type=file onchange="showFileContents(event.target)">
<pre>Select a file...</pre>
<script>
function showFileContents(input) {
var reader = new FileReader();
reader.onload = function() {
document.querySelector("pre").textContent = reader.result;
};
reader.readAsText(input.files[0], "UTF-8");
}
</script>
data: URL<img src>, <audio src>,
background-image, ...
<img> <input type=file onchange="showImage(event.target)">
<script>
function showImage(input) {
var reader = new FileReader();
reader.onload = function() {
document.querySelector("img").src = reader.result;
};
reader.readAsDataURL(input.files[0]);
}
</script>
FileReader.readAsArrayBuffer() will produce an ArrayBufferUint8Array – to access the bytes in the bufferSelect a file...
function showFileBytes(input) {
var reader = new FileReader();
reader.onload = function() {
var s = "", bytes = new Uint8Array(reader.result);
for (var i = 0; i < Math.max(bytes.length, 48); i++)
s += (bytes[i] < 16 ? "0" : "") + bytes[i].toString(16) + " ";
document.querySelector("pre").textContent = s + "..";
};
reader.readAsArrayBuffer(input.files[0]);
}
function showReorientedImage(input) {
// [ ... read and display image using a data: URL as before ... ]
var reader = new FileReader();
reader.onload = function() {
// Using https://github.com/jseidelin/exif-js/ ...
var orientation = readEXIFTags(reader.result).Orientation;
var rotation = [0, 0, 0, 180, 180, 90, 90, -90, -90];
document.querySelector("img").style.transform =
"rotate(" + (rotation[orientation] || 0) + "deg)";
};
reader.readAsArrayBuffer(input.files[0]);
}
data: URLs are cool, but can be slow – might be megabytes longblob: URLs are like pointers to a File (or other Blob) objectvar file = input.files[0]; var url = URL.createObjectURL(file); // "blob:d719e6ca-3d89-464e-..." img.src = url; // If finished before the page is closed: URL.revokeObjectURL(url);
File to XMLHttpRequest.send()File out of a DataTransfer object
when handling a drop event<input type=file>,
present your own UI, and invoke the open dialog with a .click()<a href="blob:..." download> might help here![]() |
![]() |
![]() |
![]() |
![]() |
| 6+ | 3.6+ | 10 | 11+ | 6 |
But good polyfills are available!
Cameron McCormack, Mozilla · @heycam