SHARE
TWEET

Untitled

a guest Sep 19th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function readImageAsync(file) {
  2.   return new Promise((resolve, reject) => {
  3.     const reader = new FileReader();
  4.  
  5.     reader.onload = () => {
  6.       const image = new Image();
  7.       image.src = reader.result;
  8.       image.onload = () => {
  9.         resolve(image);
  10.       };
  11.     };
  12.  
  13.     reader.onerror = reject;
  14.  
  15.     reader.readAsDataURL(file);
  16.   });
  17. }
  18.  
  19. function isImage(file) {
  20.   const acceptedImageTypes = ['image/gif', 'image/jpeg', 'image/png', 'image/bmp', 'image/tiff'];
  21.   return file && acceptedImageTypes.includes(file.type);
  22. }
  23.  
  24. // `file` is a selected file on the user's input.
  25. if (isImage(file)) {
  26.   // Since it recovers an Image object, it must be an image file.
  27.   const imageData = await readImageAsync(file);
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top