muiltpul file upload with a que

Discussion in 'JavaScript' started by function, Aug 24, 2007.

  1. #1
    Hi I made a page using javascript that uploads files one at i time using a iframe and javascript creates a queue of them to upload. its not fully finished yet works in fire fox and with a few mods will work in all browsers, except I can't get it to work in IE 7 its doing my head in, any one know why?
    
    function MultiUpload( list_target , form_target){
    
    	this.list_target = list_target;
    	this.form_target = form_target;
    	this.uploading = false ;
    	this.trackerArray = new Array();
    	this.id = 0;
    
    		this.makeForm = function(){
    		
    		var formContainer = this.form_target;
    
    		var newForm=document.createElement("form");
    		
    		newForm.name = 'file_' + this.id;
    		newForm.action = 'imageUpload.php';
    		newForm.method = 'post';
    		newForm.enctype = 'multipart/form-data';
    		newForm.target = 'imageUploadFrame';
    		
    
    		// New file input
    		var new_element = document.createElement('input');
    		new_element.type = 'file';
    		new_element.name = 'images[]';
    		//var new_element = document.createElement('<input type="file" name="images[]"/>');
    		
    		newForm.appendChild(new_element);
    		
    		new_element.onchange = function(){
    				multiUpload.userInput( this );
    			};
    		
    		formContainer.appendChild( newForm );
    		
    	};
    	
    	this.userInput = function( inputBox ){
    		
    		file = this.getFileName( inputBox.value );
    		//file = inputBox.value;
    		
    		if ( file != ' ' )
    		{
    			var form = inputBox.parentNode;
    			
    			if ( this.uploading )
    			{	
    				this.addTo( file , form , "Waiting" );	
    			}
    			else
    			{
    				this.addTo( file , form , "Uploading" );
    				this.uploading = true;
    				//document.file_0.submit()
    				//document.file_0.submit();
    				form.submit();
    				//alert("uploading" + form);
    			}
    			this.hideForm( form );
    			this.makeForm();
    			this.id++;
    		}
    		else
    		{
    			alert("This is not a jpeg image.\njpeg images only.")
    		}
    	};
    		
    	this.addTo = function( file, form, status ){
    		
    		this.addToArray( file , form , status );
    		this.addToList( file , status );
    	};
    		
    	this.hideForm = function( formToHide ){
    	
    		formToHide.style.position = 'absolute';
    		formToHide.style.left = '-1000px';
    	};
    		
    	this.addToList = function( file, status ){
    		
    		var new_row = document.createElement( 'div' );
    		new_row.setAttribute("class","fileList");
    
    		new_row.innerHTML = file + " " + status;
    
    		// Add it to the list
    		this.list_target.appendChild( new_row );
    	};
    		
    	this.callBk = function( file , message ){
    	
    		this.update( file , message);
    		this.upload();
    	};
    	
    	this.upload = function(){
    	
    		for(i = 0; i < this.trackerArray.length; i++)
    		{
    			if ( this.trackerArray[i]["status"].toString() == "Waiting" )
    			{
    				this.uploading = true;
    				this.trackerArray[i]["form"].submit();
    				this.trackerArray[i]["status"] = "Uploading";
    				this.updateList( this.trackerArray[i]["file"] , this.trackerArray[i]["status"]);
    				return;
    			}
    		}
    		this.uploading = false;
    	};
    	
    	this.addToArray = function( file, form, status ){
    	
    		alert(form);
    		this.trackerArray[this.id] = new Array();
    		this.trackerArray[this.id]["file"] = file;
    		this.trackerArray[this.id]["form"] = form;
    		this.trackerArray[this.id]["status"] = status;
    	};	
    		
    	this.update = function( fileName , message){
    	
    		this.updateArray(  fileName , message );
    		this.updateList( fileName , message );
    	};
    		
    	this.updateArray = function( fileName , message){
    	
    		for(i = 0; i < this.trackerArray.length; i++)
    		{
    			if ( this.trackerArray[i]["file"].toString() == fileName.toString() )
    			{
    				this.trackerArray[i]["status"] = message;
    				return;
    			}
    		}
    	};
    		
    	this.updateList = function( file, status )
    	{
    
    		var theChildren = this.list_target.childNodes;	
    		
    		for(i = 0; i < theChildren.length; i++)
    		{
    			text = theChildren[i].innerHTML;
    			textArray = text.split(/ /);
    			string = textArray[0];
    			if ( string == file )
    			{
    				theChildren[i].innerHTML = file + " " + status;
    				return;
    			}		
    		}
    	};
    		
    	this.getFileName = function( pathString ){
    
    		if (pathString.match(/\\/))
    		{
    			array = pathString.split(/\\/);
    			if (pathString.match(/.*\.(jpeg|jpg)$/))
    				return( array[(array.length -1)] );
    		}
    		else if ( pathString.match(/\//))
    		{
    			if ( pathString.match(/.*\.(jpeg|jpg)$/ ))
    				return( array[(array.length -1)] );
    		}
    		return ' ';
    	};
    }
    
    Code (markup):
    http://extra.shu.ac.uk/shucanoe/dev/form.html

    ggrrrrrrrr IE
     
    function, Aug 24, 2007 IP
  2. function

    function Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    oh IE just submits the file name, which is not very useful.
     
    function, Aug 24, 2007 IP
  3. function

    function Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well after more investigation a found that IE handles inserting of the name into the div differently. http://www.thunderguy.com/semicolon/2005/05/23/setting-the-name-attribute-in-internet-explorer/

    So i added the little function on there so it now adds the name right.
    I also learn how to do a little bit of debugging in IE. I downloaded the Internet Explore developer tool bar from http://www.microsoft.com/downloads/thankyou.aspx?familyId=e59c3964-672d-4511-bb3e-2d5e1db91038&displayLang=en its like the firebug add on for firebug.

    I have nailed down the problem IE submits the form using application/x-www-form-urlencoded inside of multipart/form-data. this is really strange, even in Internet Explore DOM it shows the form as multipart/form-data but still submits it has urlencoded. Anyone else had this problem?
    Thank you for any help.
     
    function, Aug 25, 2007 IP