daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Sep 18th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.     <meta charset='UTF-8'>
  6.     <link rel='shortcut icon' href=''>
  7.     <title>read as an array buffer</title>
  8. </head>
  9.  
  10. <body>
  11.     <form id='fileUploadForm' enctype='multipart/form-data'>
  12.         <input type='file' name='file' id='file-input' />
  13.     </form>
  14.     <div id='display-types'></div>
  15. </body>
  16. <script>
  17.     const mimeTypes = {
  18.         '4f676753': 'video/ogg',
  19.         '00000020': 'video/mp4',
  20.         '00000014': 'video/quicktime',
  21.         '3026b275': 'video/x-ms-wmv',
  22.         '1a45dfa3': 'video/webm'
  23.     };
  24.     const fileInput = document.getElementById('file-input');
  25.     fileInput.addEventListener('change', (event) => {
  26.         const videoFile = event.target.files[0];
  27.         const fileReader = new FileReader();
  28.         fileReader.onload = () => {
  29.             const arrayBuffer = fileReader.result;
  30.             const unsignedInteger8View = new Uint8Array(arrayBuffer);
  31.             let magicNumbers = '';
  32.             unsignedInteger8View.forEach((byte) => {
  33.                 if (byte.toString().length === 1) {
  34.                     // appending below to create a fixed string length of two characters/UTF-8 representation
  35.                     magicNumbers += "0" + byte.toString(16)
  36.                 } else {
  37.                     magicNumbers += byte.toString(16)
  38.                 }
  39.             });
  40.             const display = document.getElementById('display-types');
  41.             display.innerHTML = `The MIMEtype for this video is: ${mimeTypes[bytes]}`
  42.         }
  43.         const first4Bytes = videoFile.slice(0, 4);
  44.         fileReader.readAsArrayBuffer(first4Bytes);
  45.     });
  46. </script>
  47.  
  48. </html>
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