Advertisement
metalx1000

Load Multiple Images Locally

Jul 20th, 2013
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.65 KB | None | 0 0
  1. <!--Based on code from http://www.html5rocks.com/en/tutorials/file/dndfiles/ -->
  2. <html>
  3.     <head>
  4.         <style>
  5.           .thumb {
  6.             height: 75px;
  7.             border: 1px solid #000;
  8.             margin: 10px 5px 0 0;
  9.           }
  10.         </style>
  11.     </head>
  12.     <body>
  13.         <input type="file" id="files" name="files[]" multiple />
  14.         <hr>
  15.         <div id="images"></div>
  16.  
  17.  
  18.         <script>
  19.           function loadFiles(event) {
  20.             var files = event.target.files; // FileList object
  21.             // Loop through the FileList and render image files as thumbnails.
  22.             for (var i = 0; f = files[i]; i++) {
  23.        
  24.               // Only process image files.
  25.               if (!f.type.match('image.*')) {
  26.                 continue;
  27.               }
  28.        
  29.               var reader = new FileReader();
  30.        
  31.               // Closure to capture the file information.
  32.               reader.onload = (function(theFile) {
  33.                 return function(e) {
  34.                   // Render thumbnail.
  35.                     var a = '<a href="' + e.target.result + '" target="_blank">';
  36.                     var b = '<img class="thumb" src="' + e.target.result;
  37.                    var c = '" title="' + escape(theFile.name) + '"/></a>';
  38.                   document.getElementById('images').innerHTML+=a+b+c;
  39.                 };
  40.               })(f);
  41.        
  42.               // Read in the image file as a data URL.
  43.               reader.readAsDataURL(f);
  44.             }
  45.           }
  46.        
  47.           document.getElementById('files').addEventListener('change', loadFiles, false);
  48.         </script>
  49.     </body>
  50. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement