diff --git a/ui/scripts/ui/dialog.js b/ui/scripts/ui/dialog.js index a510be92956..ca5ce8bcb5c 100644 --- a/ui/scripts/ui/dialog.js +++ b/ui/scripts/ui/dialog.js @@ -476,9 +476,13 @@ } else if (field.isFileUpload) { $input = $('').attr({ type: 'file', - name: 'files[]', - 'data-url': '/upload.jsp' + name: 'files[]' }).appendTo($value); + + // Add events + $input.change(function(event) { + $form.data('files', event.target.files); + }); } else if (field.isTokenInput) { // jquery.tokeninput.js isAsync = true; @@ -678,12 +682,57 @@ } } - args.after({ - data: data, - ref: args.ref, // For backwards compatibility; use context - context: args.context, - $form: $form - }); + var uploadFiles = function() { + // START A LOADING SPINNER HERE + + // Create a formdata object and add the files + var data = new FormData(); + $.each($form.data('files'), function(key, value) + { + data.append(key, value); + }); + + $.ajax({ + url: '/client/upload.json', + type: 'POST', + data: data, + 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(typeof data.error === 'undefined') + { + // Success so call function to process the form + debugger; + //submitForm(event, data); + } + else + { + // Handle errors here + console.log('ERRORS: ' + data.error); + } + }, + error: function(jqXHR, textStatus, errorThrown) + { + // Handle errors here + console.log('ERRORS: ' + textStatus); + // STOP LOADING SPINNER + } + }); + }; + + if ($form.data('files')) { + uploadFiles(); + } else { + args.after({ + data: data, + ref: args.ref, // For backwards compatibility; use context + context: args.context, + $form: $form + }); + } return true; };