Jump to content

jQuery File Uploader


jnymris

Recommended Posts

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 by Jonathon Morris
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...