Advertisement
Guest User

Untitled

a guest
May 27th, 2013
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     var input = document.getElementById("images")
  3.  
  4.  
  5.     function showUploadedItem (file, id) {
  6.            
  7.         var reader = new FileReader();
  8.         reader.onload = (function(theFile) {
  9.                 return function(e) {
  10.                     var template = '<li>'+
  11.                                         '<img src="'+e.target.result+'">'+
  12.                                         '<br />'+
  13.                                         '<progress min="0" max="100" value="0" id="'+id+'"></progress>'+
  14.                                    '</li>';    
  15.                     $("#image-list").append(template);
  16.                 };
  17.         })(file);
  18.        
  19.         reader.readAsDataURL(file);    
  20.     }  
  21.  
  22.  
  23.     input.addEventListener("change", function (evt) {
  24.    
  25.         for (var i=0, j=this.files.length; i<j; i++) {
  26.             file = this.files[i];
  27.            
  28.             formdata = new FormData();         
  29.             formdata.append("images[]", file);
  30.                    
  31.             var xhr = new XMLHttpRequest(),
  32.                 upload = xhr.upload,
  33.                 id = "progress_" + Math.floor((Math.random() * 100000));
  34.                
  35.                 upload.addEventListener("loadstart", function(e){
  36.                     showUploadedItem(file, this.id);
  37.                 });/*          
  38.                 xhr.addEventListener('progress', function(e) {
  39.    
  40.                 }, false);*/
  41.                 upload.id = id;
  42.                 upload.onprogress = function(e) {
  43.                     var done = e.position || e.loaded, total = e.totalSize || e.total;
  44.                     $("#" + this.id).attr('value',  Math.floor((e.loaded / e.total) * 100) )
  45.                     //console.log( this.id );
  46.                     //alert(this.id);
  47.                 };
  48.                 /*xhr.onreadystatechange = function(e) {
  49.                     if ( 4 == this.readyState ) {
  50.                         console.log(['xhr upload complete', e]);
  51.                     }
  52.                 };*/
  53.                 xhr.open('post', 'upload.php', true);
  54.                 xhr.send(formdata);
  55.            
  56.         }
  57.    
  58.     }, false);
  59. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement