[javascript] Get file size, image width and height before upload

Multiple images upload with info data preview

Using HTML5 and the File API

Example using URL API

The images sources will be a URL representing the Blob object
<img src="blob:null/026cceb9-edr4-4281-babb-b56cbf759a3d">

_x000D_
_x000D_
const EL_browse  = document.getElementById('browse');_x000D_
const EL_preview = document.getElementById('preview');_x000D_
_x000D_
const readImage  = file => {_x000D_
  if ( !(/^image\/(png|jpe?g|gif)$/).test(file.type) )_x000D_
    return EL_preview.insertAdjacentHTML('beforeend', `Unsupported format ${file.type}: ${file.name}<br>`);_x000D_
_x000D_
  const img = new Image();_x000D_
  img.addEventListener('load', () => {_x000D_
    EL_preview.appendChild(img);_x000D_
    EL_preview.insertAdjacentHTML('beforeend', `<div>${file.name} ${img.width}×${img.height} ${file.type} ${Math.round(file.size/1024)}KB<div>`);_x000D_
    window.URL.revokeObjectURL(img.src); // Free some memory_x000D_
  });_x000D_
  img.src = window.URL.createObjectURL(file);_x000D_
}_x000D_
_x000D_
EL_browse.addEventListener('change', ev => {_x000D_
  EL_preview.innerHTML = ''; // Remove old images and data_x000D_
  const files = ev.target.files;_x000D_
  if (!files || !files[0]) return alert('File upload not supported');_x000D_
  [...files].forEach( readImage );_x000D_
});
_x000D_
#preview img { max-height: 100px; }
_x000D_
<input id="browse" type="file" multiple>_x000D_
<div id="preview"></div>
_x000D_
_x000D_
_x000D_

Example using FileReader API

In case you need images sources as long Base64 encoded data strings
<img src="data:image/png;base64,iVBORw0KGg... ...lF/++TkSuQmCC=">

_x000D_
_x000D_
const EL_browse  = document.getElementById('browse');_x000D_
const EL_preview = document.getElementById('preview');_x000D_
_x000D_
const readImage = file => {_x000D_
  if ( !(/^image\/(png|jpe?g|gif)$/).test(file.type) )_x000D_
    return EL_preview.insertAdjacentHTML('beforeend', `<div>Unsupported format ${file.type}: ${file.name}</div>`);_x000D_
_x000D_
  const reader = new FileReader();_x000D_
  reader.addEventListener('load', () => {_x000D_
    const img  = new Image();_x000D_
    img.addEventListener('load', () => {_x000D_
      EL_preview.appendChild(img);_x000D_
      EL_preview.insertAdjacentHTML('beforeend', `<div>${file.name} ${img.width}×${img.height} ${file.type} ${Math.round(file.size/1024)}KB</div>`);_x000D_
    });_x000D_
    img.src = reader.result;_x000D_
  });_x000D_
  reader.readAsDataURL(file);  _x000D_
};_x000D_
_x000D_
EL_browse.addEventListener('change', ev => {_x000D_
  EL_preview.innerHTML = ''; // Clear Preview_x000D_
  const files = ev.target.files;_x000D_
  if (!files || !files[0]) return alert('File upload not supported');_x000D_
  [...files].forEach( readImage );_x000D_
});
_x000D_
#preview img { max-height: 100px; }
_x000D_
<input id="browse" type="file"  multiple>_x000D_
<div id="preview"></div>_x000D_
  
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to dimensions

Get file size, image width and height before upload Get image dimensions Numpy array dimensions How to get screen dimensions as pixels in Android Total width of element (including padding and border) in jQuery

Examples related to image-size

XXHDPI and XXXHDPI dimensions in dp for images and icons in android Get file size, image width and height before upload Change image size via parent div Check image width and height before upload with Javascript Specifying width and height as percentages without skewing photo proportions in HTML