www.webdeveloper.com
Results 1 to 2 of 2

Thread: File upload progress issue.

  1. #1
    Join Date
    Oct 2012
    Posts
    4

    File upload progress issue.

    Hi,

    In firefox, and chrome, I have a progress bar when someone uploads a file, however the same code doesn't work in IE (surprise surprise), it fails on:

    Code:
    var file = this.files[0];
    My question is how do I do the equivalent for IE? Pointing me to the documentation would best.

    code is:
    Code:
    function uploadImage(){
    			$('#percent').progressbar({
    				value: 0
    			});
    			$( "#dialog" ).dialog({
    			resizable: false,
    			height:150,
    			modal: true,
    			buttons: {
    				Cancel: function() {
    					if (xhr){
    						xhr.abort();
    						xhr = null;
    					}
    					$( this ).dialog( "close" );
    				}
    			}
    			});
    			document.getElementById('photo').addEventListener('change', function(e) {
    				var file = this.files[0];
    				var formdata = new FormData();
    				formdata.append('file', file);
    				xhr = new XMLHttpRequest();
    				xhr.file = file; 
    				xhr.addEventListener('progress', function(e) {
    					var done = e.position || e.loaded, total = e.totalSize || e.total;
    					$('#percent').progressbar('value', (Math.floor(done/total*1000)/10));
    				}, false);
    				if ( xhr.upload ) {
    					xhr.upload.onprogress = function(e) {
    						var done = e.position || e.loaded, total = e.totalSize || e.total;
    						$('#percent').progressbar('value', (Math.floor(done/total*1000)/10));
    
    					};
    				}
    				xhr.onreadystatechange = function(e) {
    					if ( 4 == this.readyState ) {
    						console.log(this);
    						$('#src').val(this.responseText);
    						$('#dialog').dialog('close');
    						xhr = null;
    					}
    				};
    				xhr.open('post', '/index/fileupload', true);
    				xhr.send(formdata);
    			}, false);
    		}

  2. #2
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    Which versions of IE did you tried with.
    The addEventListener does not work in IE8 and earlier versions.

    Try:

    if(document.addEventListener){
    xhr.addEventListener('progress', function(e) {
    var done = e.position || e.loaded, total = e.totalSize || e.total;
    $('#percent').progressbar('value', (Math.floor(done/total*1000)/10));
    }, false);
    }
    else {
    xhr.attachEvent('progress', function(e) {
    var done = e.position || e.loaded, total = e.totalSize || e.total;
    $('#percent').progressbar('value', (Math.floor(done/total*1000)/10));
    });

    }


    Do the same for
    document.getElementById('photo').addEventListener('change', ....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles