File upload issue when more than 1 file field
Hi,
I have a form with 5 <input type="file" .....> fields each with different names. I have the form in a bootstrap modal so am using jquery to post my form and on successfull uploading I will close modal and give a message saying files uploaded successfully.
As the user adds a file I want to add it to some sort of structure then when the user clicks the submit button I want to loop through this structure and upload the files, I have used the functionality at
http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax and my code works fine for 1 file field. The issue is when I want to upload more than 1 file. I think the issue is to do with the following code.
=======
- // Variable to store your files
- var files;
-
- // Add events
- $('input[type=file]').on('change', prepareUpload);
-
- // Grab the files and set them to our variable
- function prepareUpload(event)
- {
- files = event.target.files;
-
- }
=====
When I add a file it goes into the files structure fine but then I add another it overwrites the previous one.
I have tried using push to add the file to the files variable but then uploader doesn't seem to get the file to upload ie. contents of the file dont get included in the upload.
If I have selected 3 files to be uploaded I would expect the files variable to have 3 items.
My whole block of code is as follows.
=====
- <script>
-
- $(document).ready(function(){
- $("input[type=file]").nicefileinput();
-
- // Variable to store your files
- var files;
-
- // Add events
- $('input[type=file]').on('change', prepareUpload);
-
- // Grab the files and set them to our variable
- function prepareUpload(event)
- {
- files = event.target.files;
- alert(files);
- }
-
- $('#sub_button').off().on('click', uploadFiles);
-
- // Catch the form submit and upload the files
- function uploadFiles(event)
- {
- event.stopPropagation(); // Stop stuff happening
- event.preventDefault(); // Totally stop stuff happening
-
- // START A LOADING SPINNER HERE
-
- // Create a formdata object and add the files
- var filedata = new FormData();
-
- if(typeof files != 'undefined'){
- $.each(files, function(key, value)
- {
- filedata.append(key, value);
- });
- }
-
- if(typeof files != 'undefined')
- {
- var cpCompanyComplianceRequirementID = $('#cpCompanyComplianceRequirementID').val();
-
- $.post('cfdiv/reroute.cfm?nolayout&type=87&cpCompanyComplianceRequirementID=' + cpCompanyComplianceRequirementID)
- .done(function(data) {
-
- if (data > 0)
- {
- var all_good = 1;
-
- $.each(files, function(key, value)
- {
- $.ajax({
- url: 'cfdiv/reroute.cfm?nolayout&type=86&cpCompanyComplianceRequirementResponseID=' + data,
- type: 'POST',
- data: filedata,
- cache: false,
- dataType: 'json',
- processData: false, // Don't process the files
- contentType: false, // Set content type to false as jQuery will tell the server its a query string request
- success: function(data, textStatus, jqXHR)
- {
- if(data == 0)
-
- {
- all_good = 0;
- }
- },
- error: function(jqXHR, textStatus, errorThrown)
- {
- all_good = 0;
- }
- });
- });
-
- if (all_good == 1){
- // Success
- $('#modal_compliance_upload').modal('hide');
- toastr.success('Files uploaded successfully','Success');
-
- }
- else
- {
- // Handle errors here
- console.log('ERRORS: ' + data);
- toastr.error('Your files were not uploaded successfully. Please try again.','Error');
- }
-
-
- }
- else
- {
- toastr.error('Documents were not saved. Please try again.','Error');
- }
-
-
- });
-
-
- }
- else
- {
- toastr.error('No files selected for upload. Please select some files and try again.','Error');
- }
-
- }
- });
- </script>
=====
I have left in all the extra stuff there but as I say it works perfectly with 1 file but any time I add 2 or more only 1 file gets uploaded.
Anyone have any ideas? I've tried various things all day but to no avail.
Thanks,
Paul