js: Add fallback in case XHR file uploads are not possible

refs #8758
This commit is contained in:
Johannes Meyer 2015-07-20 10:52:28 +02:00
parent be88683c19
commit 37b87eb284
2 changed files with 40 additions and 0 deletions

View File

@ -269,6 +269,17 @@
url = icinga.utils.addUrlParams(url, dataObj);
} else {
if (encoding === 'multipart/form-data') {
if (typeof window.FormData === 'undefined') {
icinga.loader.submitFormToIframe($form, url, $target);
// Disable all form controls to prevent resubmission as early as possible.
// (This relies on native form submission, so using setTimeout is the only possible solution)
setTimeout(function () {
$form.find(':input:not(:disabled)').prop('disabled', true);
}, 0);
return true;
}
data = new window.FormData($form[0]);
} else {
data = $form.serializeArray();

View File

@ -140,6 +140,35 @@
return req;
},
/**
* Mimic XHR form submission by using an iframe
*
* @param {object} $form The form being submitted
* @param {string} action The form's action URL
* @param {object} $target The target container
*/
submitFormToIframe: function ($form, action, $target) {
var self = this;
$form.prop('action', self.icinga.utils.addUrlParams(action, {
'_disableLayout': true
}));
$form.prop('target', 'fileupload-frame-target');
$('#fileupload-frame-target').on('load', function (event) {
var $frame = $(event.target);
// Fetch the frame's new content, paste it into the target..
self.renderContentToContainer(
$frame.contents().find('body').html(),
$target,
'replace'
);
$frame.prop('src', 'about:blank'); // ..and clear the frame's dom
$frame.off('load'); // Unbind the event as it's set on demand
});
},
/**
* Create an URL relative to the Icinga base Url, still unused
*