Skip to content

Commit f243ae1

Browse files
committed
Add front-end file uploader
1 parent 627f5a6 commit f243ae1

2 files changed

Lines changed: 97 additions & 6 deletions

File tree

‎ui/scripts/templates.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@
107107
title: 'label.action.register.template',
108108
docID: 'helpNetworkOfferingName',
109109
preFilter: cloudStack.preFilter.createTemplate,
110+
fileUpload: {
111+
getURL: function(args) {
112+
args.response.success({
113+
url: 'http://10.223.183.3/test-upload.php'
114+
});
115+
},
116+
postUpload: function(args) {
117+
// Called when upload is done to do
118+
// verification checks;
119+
// i.e., poll the server to verify successful upload
120+
//
121+
// success() will close the dialog and call standard action
122+
// error() will keep dialog open if user wants to re-submit
123+
args.response.success();
124+
}
125+
},
110126
fields: {
111127
name: {
112128
label: 'label.name',
@@ -115,6 +131,10 @@
115131
required: true
116132
}
117133
},
134+
templateFileUpload: {
135+
label: 'Select a file',
136+
isFileUpload: true
137+
},
118138
description: {
119139
label: 'label.description',
120140
docID: 'helpRegisterTemplateDescription',

‎ui/scripts/ui/dialog.js‎

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,16 @@
473473
if (field.defaultValue) {
474474
$input.val(strOrFunc(field.defaultValue));
475475
}
476+
} else if (field.isFileUpload) {
477+
$input = $('<input>').attr({
478+
type: 'file',
479+
name: 'files[]'
480+
}).appendTo($value);
481+
482+
// Add events
483+
$input.change(function(event) {
484+
$form.data('files', event.target.files);
485+
});
476486
} else if (field.isTokenInput) { // jquery.tokeninput.js
477487
isAsync = true;
478488

@@ -672,12 +682,73 @@
672682
}
673683
}
674684

675-
args.after({
676-
data: data,
677-
ref: args.ref, // For backwards compatibility; use context
678-
context: args.context,
679-
$form: $form
680-
});
685+
var uploadFiles = function() {
686+
$form.prepend($('<div>').addClass('loading-overlay'));
687+
args.form.fileUpload.getURL({
688+
formData: data,
689+
context: args.context,
690+
response: {
691+
success: function(successArgs) {
692+
//
693+
// Move file field into iframe; keep visible for consistency
694+
//
695+
var $uploadFrame = $('<iframe>');
696+
var $frameForm = $('<form>').attr({
697+
method: 'POST',
698+
action: successArgs.url,
699+
enctype: 'multipart/form-data'
700+
});
701+
var $file = $form.find('input[type=file]');
702+
var $field = $file.closest('.form-item .value');
703+
704+
$uploadFrame.css({ width: $field.outerWidth(), height: $field.height() }).show();
705+
$frameForm.append($file);
706+
$field.append($uploadFrame);
707+
$uploadFrame.contents().find('html body').append($frameForm);
708+
$frameForm.submit(function() {
709+
$uploadFrame.load(function() {
710+
args.form.fileUpload.postUpload({
711+
formData: data,
712+
context: args.context,
713+
response: {
714+
success: function() {
715+
args.after({
716+
data: data,
717+
ref: args.ref, // For backwards compatibility; use context
718+
context: args.context,
719+
$form: $form
720+
});
721+
},
722+
error: function(msg) {
723+
$form.find('.loading-overlay').remove();
724+
cloudStack.dialog.error({ message: msg });
725+
}
726+
}
727+
});
728+
});
729+
return true;
730+
});
731+
$frameForm.submit();
732+
},
733+
error: function(msg) {
734+
cloudStack.dialog.error({ message: msg });
735+
}
736+
}
737+
});
738+
};
739+
740+
if ($form.data('files')) {
741+
uploadFiles();
742+
743+
return false;
744+
} else {
745+
args.after({
746+
data: data,
747+
ref: args.ref, // For backwards compatibility; use context
748+
context: args.context,
749+
$form: $form
750+
});
751+
}
681752

682753
return true;
683754
};

0 commit comments

Comments
 (0)