Jump to content

Dynamic File upload system


baconbeastnz

Recommended Posts

Hi there, I am creating (and thought I had finished) a dynamic upload system. Dynamic in that once you have onChanged() all <input type="file"> fields, 2 more pop up, so you can upload as many files as you want.

 

Each time I pop out 2 more fields, I simply regen the fields again with 2 more and set the innerhtml of a div. Doing this removes the values of the already used fields. My plan was to simply maintain the values of the fields by setting the value="" parameter, however this is not possible, due to the security implications!! GRRR

 

I need a way to keep the used fields unchanged so they retain their values, then just pop on 2 extra ones, any ideas?

Link to comment
Share on other sites

The only way I know of to append without replacing content is to use the DOM API.

 

 

<html>
<title>Dynamic Download Fields</title>
<script language="javascript">
<!--
	function AddSlot(input_element) {
		var c = input_element.parentNode;
		if(c != null) {
			var newFile = document.createElement('input');
			var lineBreak = document.createElement('br');
			newFile.type="file";
			newFile.name="files[]";
			newFile.setAttribute('onchange', "AddSlot(this)");
			c.appendChild(newFile);
			c.appendChild(lineBreak);
			return true;
		}
		return false;
	}
//-->
</script>
</html>
<body>
<form>
	<div id="file_container">
		<input type="file" name="files[]" onchange="AddSlot(this);">
		<br>
	</div>
</form>
</body>
</html>

 

 

For example.

 

 

With AJAX though that's going to get more complex since obviously using a preexisting HTML string complicates things.  I think jQuery can append an HTML string without replacement, or you could change how your AJAX is returned.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.