Skip to content

Commit deec6b9

Browse files
committed
Allow to pass only a fileInput to add and send API calls, without having to define the files list manually.
1 parent 2bc9a5f commit deec6b9

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

js/jquery.fileupload.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin 5.11.3
2+
* jQuery File Upload Plugin 5.12
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -776,19 +776,30 @@
776776
}
777777
},
778778

779+
_getFileInputFiles: function (fileInput) {
780+
fileInput = $(fileInput);
781+
var files = $.each($.makeArray(fileInput.prop('files')), this._normalizeFile),
782+
value;
783+
if (!files.length) {
784+
value = fileInput.prop('value');
785+
if (!value) {
786+
return [];
787+
}
788+
// If the files property is not available, the browser does not
789+
// support the File API and we add a pseudo File object with
790+
// the input value as name with path information removed:
791+
files = [{name: value.replace(/^.*\\/, '')}];
792+
}
793+
return files;
794+
},
795+
779796
_onChange: function (e) {
780797
var that = e.data.fileupload,
781798
data = {
782-
files: $.each($.makeArray(e.target.files), that._normalizeFile),
783799
fileInput: $(e.target),
784800
form: $(e.target.form)
785801
};
786-
if (!data.files.length) {
787-
// If the files property is not available, the browser does not
788-
// support the File API and we add a pseudo File object with
789-
// the input value as name with path information removed:
790-
data.files = [{name: e.target.value.replace(/^.*\\/, '')}];
791-
}
802+
data.files = that._getFileInputFiles(data.fileInput);
792803
if (that.options.replaceFileInput) {
793804
that._replaceFileInput(data.fileInput);
794805
}
@@ -925,7 +936,11 @@
925936
if (!data || this.options.disabled) {
926937
return;
927938
}
928-
data.files = $.each($.makeArray(data.files), this._normalizeFile);
939+
if (data.fileInput && !data.files) {
940+
data.files = this._getFileInputFiles(data.fileInput);
941+
} else {
942+
data.files = $.each($.makeArray(data.files), this._normalizeFile);
943+
}
929944
this._onAdd(null, data);
930945
},
931946

@@ -936,7 +951,11 @@
936951
// The method returns a Promise object for the file upload call.
937952
send: function (data) {
938953
if (data && !this.options.disabled) {
939-
data.files = $.each($.makeArray(data.files), this._normalizeFile);
954+
if (data.fileInput && !data.files) {
955+
data.files = this._getFileInputFiles(data.fileInput);
956+
} else {
957+
data.files = $.each($.makeArray(data.files), this._normalizeFile);
958+
}
940959
if (data.files.length) {
941960
return this._onSend(null, data);
942961
}

test/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin Test 6.9
2+
* jQuery File Upload Plugin Test 6.9.1
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -480,8 +480,8 @@ $(function () {
480480
ok(true, 'Triggers change callback');
481481
strictEqual(
482482
data.files.length,
483-
1,
484-
'Creates pseudo File object'
483+
0,
484+
'Returns empty files list'
485485
);
486486
},
487487
add: $.noop

0 commit comments

Comments
 (0)