Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Parsing Browse to File Name

    Code:
    <html>
    	<head>
    	<title>ServiceNet Upload Existing Item</title>
    	<link rel="stylesheet" type="text/css" href="styles.css"/>
    	<script language="JavaScript">
    		function setFileName(path, fieldObj)
    		{
    		  var index = fieldObj.value.lastIndexOf(".xml");
    		  if ( index > 0) {
    			var fileNameObj = document.forms[0].elements[2];
    			if (path.length > 0)
    			  fileNameObj.value = path + "\\" + fieldObj.value.substring( fieldObj.value.lastIndexOf("\\")+1 );
    			else 
    			  fileNameObj.value = fieldObj.value.substring( fieldObj.value.lastIndexOf("\\")+1 );
    		  }
    		  else {
    			alert("Only XML files can be uploaded.");
    			fieldObj.relatedElement.value = "Select a XML file.";
    			arg1.value = "Select a XML file.";
    		  }
    		}
    		
    		function fixFileName()
    		{
    			var fileNameObj = document.forms[0].elements[2];
    			if (fileNameObj.value.length == 0) {
    			   alert("Please Enter file name.");
    			   return false;
    			}
    			var index = fileNameObj.value.lastIndexOf(".xml");
    			if (fileNameObj.value.length != (index + 4) ) {
    			  alert("Only XML files are allowed to upload.");
    			  return false;        
    			}
    			if (index > 0) {
    			  if (fileNameObj.value.lastIndexOf("\\") > 0)
    				fileNameObj.value = fileNameObj.value.substring( fileNameObj.value.lastIndexOf("\\")+1 );
    			  return true;        
    			}        
    			alert("Only XML files are allowed to upload.");
    			return false;
    			
    		}
    		function fixURLFileName()
    		{
    			var fileNameObj = document.forms[1].elements[1];
    			if (fileNameObj.value.length == 0) {
    			   alert("Please enter file name.");
    			   return false;
    			}
    			var index = fileNameObj.value.lastIndexOf(".xml");
    			if (fileNameObj.value.length != (index + 4) ) {
    			  alert("Valid format is [filename-version.xml]");
    			  return false;        
    			}
    			index = fileNameObj.value.lastIndexOf("-");
    			if (index == -1) {
    			  alert("Valid format is [filename-version.xml]");
    			  return false;        
    			}
    			if (fileNameObj.value.lastIndexOf("\\") > 0)
    			  fileNameObj.value = fileNameObj.value.substring( fileNameObj.value.lastIndexOf("\\")+1 );
        	                return true;        
    			
    		}
    		var W3CDOM = (document.createElement && document.getElementsByTagName);
    
    		function init()
    		{
    			if (!W3CDOM) return;
    			var fakeFileUpload = document.createElement('span');
    			fakeFileUpload.className = 'fakefile';
    			fakeFileUpload.appendChild(document.createElement('input'));
    			var image = document.createElement('img');
    			image.src='browse.gif';
    			fakeFileUpload.appendChild(image);
    			var x = document.getElementsByTagName('input');
    			for (var i=0;i<x.length;i++)
    			{
    				if (x[i].type != 'file') continue;
    				if (x[i].getAttribute('noscript')) continue;
    				if (x[i].parentNode.className != 'fileinputs') continue;
    				x[i].className = 'file hidden';
    				var clone = fakeFileUpload.cloneNode(true);
    				x[i].parentNode.appendChild(clone);
    				x[i].relatedElement = clone.getElementsByTagName('input')[0];
    				if (x[i].value)
    					x[i].onchange();
    				x[i].onchange = function () {
    					setFileName( '', this );
    					this.relatedElement.value = this.value;
    				}
    				x[i].onKeyUp = onKeyPress = onMouseOut = function (){
    					this.relatedElement.value = this.value;
    				}
    			}
    		}
    
    	</script>
    </head>
    <body onLoad="init()">
    	<div class="tableheading" align="left" nowrap="true">ServiceNet - Upload Existing Item</div>
    	<br/><hr/><br/>
    	<form action="../servicenet/upload" enctype="multipart/form-data" method="post" onsubmit="return fixFileName()">
    		<input type="hidden" name="path" value=".\"/>
    		<span class="formfieldText">File: </span>
    		<span class="fileinputs">
    			<input type="file" class="fileUpload" name="arg1" size="40"/ onchange="setFileName( '', this );">
    		</span>
    		<span class="formfieldText">File Name: </span>
    		<input type="text" class="inputField" name="arg0" value="" size="20" length="4096"/>
    		<input type="image" class="button" src="upload.gif" border="0" name="Upload" value="Upload" />
    	</form>
    	<hr/>
    	<form action="../servicenet/uploadURL" method="post" onsubmit="return fixURLFileName()">
    		<span class="formfieldText">URL: </span>
    		<input type="text" class="inputField" name="url" size="33" length="4096"/>
    		<span class="formfieldText">File Name (e.g. test-1.0.xml) : </span>
    		<input type="text" class="inputField" name="filename" value="" size="20" length="4096"/>
    		<input type="image" class="button" src="upload.gif" border="0" name="Upload" value="Upload" />
    	</form>
    </body>
    </html>
    So, in firefox, it works right, in IE and Konqueror the xml name is not parsed to the upload input at all. Any ideas? It also acts differently in linux in firefox.

    On a side not the parsed file name stays in firefox after a refresh and only changes when you select another xml file.
    Last edited by sosuke; 01-13-2005 at 09:42 PM.

  • #2
    New Coder
    Join Date
    Nov 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, so firefox and IE count elements differently, so I went by name, then firefox didnt like onchange so i went with onblur, now its all working, woohoo

    Code:
    <html>
    	<head>
    	<title>ServiceNet Upload Existing Item</title>
    	<link rel="stylesheet" type="text/css" href="styles.css"/>
    	<script language="JavaScript">
    		function setFileName(fieldObj)
    		{
    		  var index = document.forms[0].elements['arg1'].value.lastIndexOf(".xml");
    		  if ( index != -1) {
    			var fileNameObj = document.forms[0].elements['arg0'];
    			fileNameObj.value = fieldObj.value.substring( fieldObj.value.lastIndexOf("\\")+1 );
    		  }
    		  else {
    			alert("Only XML files can be uploaded.");
    			fieldObj.relatedElement.value = "Select a XML file.";
    			arg1.value = "Select a XML file.";
    		  }
    		}
    		
    		function fixFileName()
    		{
    			var fileNameObj = document.forms[0].elements[2];
    			if (fileNameObj.value.length == 0) {
    			   alert("Please Enter file name.");
    			   return false;
    			}
    			var index = fileNameObj.value.lastIndexOf(".xml");
    			if (fileNameObj.value.length != (index + 4) ) {
    			  alert("Only XML files are allowed to upload.");
    			  return false;        
    			}
    			if (index > 0) {
    			  if (fileNameObj.value.lastIndexOf("\\") > 0)
    				fileNameObj.value = fileNameObj.value.substring( fileNameObj.value.lastIndexOf("\\")+1 );
    			  return true;        
    			}        
    			alert("Only XML files are allowed to upload.");
    			return false;
    			
    		}
    		function fixURLFileName()
    		{
    			var fileNameObj = document.forms[1].elements[1];
    			if (fileNameObj.value.length == 0) {
    			   alert("Please enter file name.");
    			   return false;
    			}
    			var index = fileNameObj.value.lastIndexOf(".xml");
    			if (fileNameObj.value.length != (index + 4) ) {
    			  alert("Valid format is [filename-version.xml]");
    			  return false;        
    			}
    			index = fileNameObj.value.lastIndexOf("-");
    			if (index == -1) {
    			  alert("Valid format is [filename-version.xml]");
    			  return false;        
    			}
    			if (fileNameObj.value.lastIndexOf("\\") > 0)
    			  fileNameObj.value = fileNameObj.value.substring( fileNameObj.value.lastIndexOf("\\")+1 );
        	                return true;        
    			
    		}
    		var W3CDOM = (document.createElement && document.getElementsByTagName);
    
    		function init()
    		{
    			document.forms[0].elements['arg0'].value = "";
    			if (!W3CDOM) return;
    			var fakeFileUpload = document.createElement('span');
    			fakeFileUpload.className = 'fakefile';
    			fakeFileUpload.appendChild(document.createElement('input'));
    			var image = document.createElement('img');
    			image.src='browse.gif';
    			fakeFileUpload.appendChild(image);
    			var x = document.getElementsByTagName('input');
    			for (var i=0;i<x.length;i++)
    			{
    				if (x[i].type != 'file') continue;
    				if (x[i].getAttribute('noscript')) continue;
    				if (x[i].parentNode.className != 'fileinputs') continue;
    				x[i].className = 'file hidden';
    				var clone = fakeFileUpload.cloneNode(true);
    				x[i].parentNode.appendChild(clone);
    				x[i].relatedElement = clone.getElementsByTagName('input')[0];
    				if (x[i].value)
    					x[i].onchange();
    				x[i].onchange = function () {
    					setFileName(this);
    					this.relatedElement.value = this.value;
    				}
    				x[i].onKeyUp = onKeyPress = onMouseOut = function (){
    					this.relatedElement.value = this.value;
    				}
    			}
    		}
    
    	</script>
    </head>
    <body onLoad="init()">
    	<div class="tableheading" align="left" nowrap="true">ServiceNet - Upload Existing Item</div>
    	<br/><hr/>
    	<form action="../servicenet/upload" enctype="multipart/form-data" method="post" onsubmit="return fixFileName()">
    		<input type="hidden" name="path" value=".\"/>
    		<span class="formfieldText">File: </span>
    		<span class="fileinputs">
    			<input type="file" class="fileUpload" name="arg1" size="40"/ onBlur="setFileName(this);">
    		</span>
    		<span class="formfieldText">File Name: </span>
    		<input type="text" class="inputField" name="arg0" value="" size="20" length="4096"/>
    		<input type="image" class="button" src="upload.gif" border="0" name="Upload" value="Upload" />
    	</form>
    	<br/>
    	<form action="../servicenet/uploadURL" method="post" onsubmit="return fixURLFileName()">
    		<span class="formfieldText">URL: </span>
    		<input type="text" class="inputField" name="url" size="33" length="4096"/>
    		<span class="formfieldText">File Name (e.g. test-1.0.xml) : </span>
    		<input type="text" class="inputField" name="filename" value="" size="20" length="4096"/>
    		<input type="image" class="button" src="upload.gif" border="0" name="Upload" value="Upload" />
    	</form>
    </body>
    </html>


  •  

    Posting Permissions

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