jnymris Posted February 28, 2013 Share Posted February 28, 2013 (edited) I'm attempting to upload files using Ajax. With the following code it will upload the first file correctly (and return the ID of that file) After changing the file to another file it will upload the second file again fine however it also re-uploads the first file thus generating two copies of the file. it is like the data is appended. I have attempted to remove the if (formdata){formdata.append("images[]",file);} which does not resolve this i have printed the value of len and it is always set to one. no matter how many files are being uploaded. (this is the problem for FireFox, Chrome and IE. Suggestions?: Javascript Code: var input = document.getElementById("files"),formdata = false;if(window.FormData){formdata = new FormData();document.getElementById("btn").style.display = "none";} function showUploadedItem (source){var list = document.getElementById("image-list"), li = document.createElement("li"),img = document.createElement("img");img.src = source;li.appendChild(img);} if(input.addEventListener){input.addEventListener("change", function (evt) {var i = 0, len = this.files.length, img, reader, file;document.getElementById("response").innterHTML = "Uploading . . ."for ( ; i < len; i++){ file = this.files[i]; if ( window.FileReader ){ reader = new FileReader(); reader.onloadend = function (e) { showUploadedItem(e.target.result); }; reader.readAsDataURL(file); } if (formdata) { formdata.append("images[]",file); }}if (formdata){$.ajax({url: "<?php echo base_url().index_page(); ?>meetings_ajax/upload_file",type: "POST",data: formdata,processData: false,contentType: false,success: function (res){document.getElementById("response").innerHTML = res;}});}}, false);}[/codeBOX] Edited March 1, 2013 by Jonathon Morris Link to comment Share on other sites More sharing options...
justsomeguy Posted February 28, 2013 Share Posted February 28, 2013 That code isn't very easy to read like that, but you would need to reset the formdata object after an upload to clear it out again. Link to comment Share on other sites More sharing options...
jnymris Posted March 1, 2013 Author Share Posted March 1, 2013 That code isn't very easy to read like that, but you would need to reset the formdata object after an upload to clear it out again. I have updated the code to be more readable. Regards Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now