Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH leverage FormData to support file upload #1854

Open
wants to merge 3 commits into
base: 2.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions client/src/legacy/LeftAndMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,13 @@ $.entwine('ss', function($) {
}

// get all data from the form
var formData = form.serializeArray();
var formData = new FormData(form[0]);
// add button action
formData.push({name: $(button).attr('name'), value:'1'});
formData.append($(button).attr('name'), '1');
// Artificial HTTP referer, IE doesn't submit them via ajax.
// Also rewrites anchors to their page counterparts, which is important
// as automatic browser ajax response redirects seem to discard the hash/fragment.
formData.push({ name: 'BackURL', value: document.URL.replace(/\/$/, '') });
formData.append('BackURL', document.URL.replace(/\/$/, ''));

// Save tab selections so we can restore them later
self.saveTabState(window.ss.tabStateUrl(), false);
Expand All @@ -552,10 +552,16 @@ $.entwine('ss', function($) {
// The returned view isn't always decided upon when the request
// is fired, so the server might decide to change it based on its own logic,
// sending back different `X-Pjax` headers and content

// Some tips on using FormData
// processData=false lets you prevent jQuery from automatically transforming the data into a query string
// setting contentType=false is imperative, since otherwise jQuery will set it incorrectly.
jQuery.ajax(jQuery.extend({
headers: {"X-Pjax" : "CurrentForm,Breadcrumbs,ValidationResult"},
url: form.attr('action'),
data: formData,
processData: false,
contentType: false,
type: 'POST',
complete: function() {
clearButton()
Expand All @@ -569,7 +575,12 @@ $.entwine('ss', function($) {
var newContentEls = self.handleAjaxResponse(data, status, xhr);
if(!newContentEls) return;

newContentEls.filter('form').trigger('aftersubmitform', {status: status, xhr: xhr, formData: formData});
var formObject = {};
// Convert to a plain object for legacy compatibility
for (var pair of formData.entries()) {
formObject[pair[0]] = pair[1];
}
newContentEls.filter('form').trigger('aftersubmitform', {status: status, xhr: xhr, formData: formObject});
}
}, ajaxOptions));
});
Expand Down
Loading