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

[WIP] File upload support with FormData object #499

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@

// Submits "remote" forms and links with ajax
handleRemote: function(element) {
var method, url, data, withCredentials, dataType, options;
var method, url, data, withCredentials, dataType, options, contentType, processData;

if (rails.fire(element, 'ajax:before')) {
withCredentials = element.data('with-credentials') || null;
Expand All @@ -115,7 +115,13 @@
if (element.is('form')) {
method = element.data('ujs:submit-button-formmethod') || element.attr('method');
url = element.data('ujs:submit-button-formaction') || element.attr('action');
data = $(element[0]).serializeArray();
if (typeof window.FormData !== 'undefined' && element.find(rails.fileInputSelector).length) {
data = new FormData(element[0]);
contentType = false;
processData = false;
} else {
data = $(element[0]).serializeArray();
}
// memoized value from clicked submit button
var button = element.data('ujs:submit-button');
if (button) {
Expand All @@ -124,6 +130,15 @@
}
element.data('ujs:submit-button-formmethod', null);
element.data('ujs:submit-button-formaction', null);
} else if (typeof window.FormData !== 'undefined' && element.is(rails.fileInputSelector)) {
method = element.data('method') || 'get';
url = element.data('url');
var formData = new FormData();
formData.append(element.attr('name'), element[0].files[0]);
formData.append(rails.csrfParam(), $('input[name="' + rails.csrfParam() + '"]').val());
data = formData;
contentType = false;
processData = false;
} else if (element.is(rails.inputChangeSelector)) {
method = element.data('method');
url = element.data('url');
Expand Down Expand Up @@ -165,6 +180,13 @@
crossDomain: rails.isCrossDomain(url)
};

if (contentType !== null) {
options.contentType = contentType;
}
if (processData !== null) {
options.processData = processData;
}

// There is no withCredentials for IE6-8 when
// "Enable native XMLHTTP support" is disabled
if (withCredentials) {
Expand Down Expand Up @@ -496,8 +518,9 @@
}

if (remote) {
// Disable this feature for modern web browsers
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
if (nonBlankFileInputs) {
if (typeof window.FormData === 'undefined' && nonBlankFileInputs) {
// Slight timeout so that the submit button gets properly serialized
// (make it easy for event handler to serialize form without disabled values)
setTimeout(function(){ rails.disableFormElements(form); }, 13);
Expand Down