Skip to content

Commit

Permalink
Allow to pass only a fileInput to add and send API calls, without hav…
Browse files Browse the repository at this point in the history
…ing to define the files list manually.
  • Loading branch information
blueimp committed Jul 6, 2012
1 parent 2bc9a5f commit deec6b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
39 changes: 29 additions & 10 deletions js/jquery.fileupload.js
@@ -1,5 +1,5 @@
/*
* jQuery File Upload Plugin 5.11.3
* jQuery File Upload Plugin 5.12
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
Expand Down Expand Up @@ -776,19 +776,30 @@
}
},

_getFileInputFiles: function (fileInput) {
fileInput = $(fileInput);
var files = $.each($.makeArray(fileInput.prop('files')), this._normalizeFile),
value;
if (!files.length) {
value = fileInput.prop('value');
if (!value) {
return [];
}
// If the files property is not available, the browser does not
// support the File API and we add a pseudo File object with
// the input value as name with path information removed:
files = [{name: value.replace(/^.*\\/, '')}];
}
return files;
},

_onChange: function (e) {
var that = e.data.fileupload,
data = {
files: $.each($.makeArray(e.target.files), that._normalizeFile),
fileInput: $(e.target),
form: $(e.target.form)
};
if (!data.files.length) {
// If the files property is not available, the browser does not
// support the File API and we add a pseudo File object with
// the input value as name with path information removed:
data.files = [{name: e.target.value.replace(/^.*\\/, '')}];
}
data.files = that._getFileInputFiles(data.fileInput);
if (that.options.replaceFileInput) {
that._replaceFileInput(data.fileInput);
}
Expand Down Expand Up @@ -925,7 +936,11 @@
if (!data || this.options.disabled) {
return;
}
data.files = $.each($.makeArray(data.files), this._normalizeFile);
if (data.fileInput && !data.files) {
data.files = this._getFileInputFiles(data.fileInput);
} else {
data.files = $.each($.makeArray(data.files), this._normalizeFile);
}
this._onAdd(null, data);
},

Expand All @@ -936,7 +951,11 @@
// The method returns a Promise object for the file upload call.
send: function (data) {
if (data && !this.options.disabled) {
data.files = $.each($.makeArray(data.files), this._normalizeFile);
if (data.fileInput && !data.files) {
data.files = this._getFileInputFiles(data.fileInput);
} else {
data.files = $.each($.makeArray(data.files), this._normalizeFile);
}
if (data.files.length) {
return this._onSend(null, data);
}
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
@@ -1,5 +1,5 @@
/*
* jQuery File Upload Plugin Test 6.9
* jQuery File Upload Plugin Test 6.9.1
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
Expand Down Expand Up @@ -480,8 +480,8 @@ $(function () {
ok(true, 'Triggers change callback');
strictEqual(
data.files.length,
1,
'Creates pseudo File object'
0,
'Returns empty files list'
);
},
add: $.noop
Expand Down

0 comments on commit deec6b9

Please sign in to comment.