Skip to content
This repository was archived by the owner on May 25, 2023. It is now read-only.

Commit d419f43

Browse files
committed
Add uniqueFilenames option.
Closes #3522.
1 parent b096b1a commit d419f43

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

js/jquery.fileupload.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@
165165
bitrateInterval: 500,
166166
// By default, uploads are started automatically when adding files:
167167
autoUpload: true,
168+
// By default, duplicate file names are expected to be handled on
169+
// the server-side. If this is not possible (e.g. when uploading
170+
// files directly to Amazon S3), the following option can be set to
171+
// an empty object or an object mapping existing filenames, e.g.:
172+
// { "image.jpg": true, "image (1).jpg": true }
173+
// If it is set, all files will be uploaded with unique filenames,
174+
// adding increasing number suffixes if necessary, e.g.:
175+
// "image (2).jpg"
176+
uniqueFilenames: undefined,
168177

169178
// Error and info messages:
170179
messages: {
@@ -449,6 +458,23 @@
449458
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
450459
},
451460

461+
_getUniqueFilename: function (name, map) {
462+
name = String(name);
463+
if (map[name]) {
464+
name = name.replace(
465+
/(?: \(([\d]+)\))?(\.[^.]+)?$/,
466+
function (_, p1, p2) {
467+
var index = p1 ? Number(p1) + 1 : 1;
468+
var ext = p2 || '';
469+
return ' (' + index + ')' + ext;
470+
}
471+
);
472+
return this._getUniqueFilename(name, map);
473+
}
474+
map[name] = true;
475+
return name;
476+
},
477+
452478
_initXHRData: function (options) {
453479
var that = this,
454480
formData,
@@ -510,11 +536,18 @@
510536
// dummy objects:
511537
if (that._isInstanceOf('File', file) ||
512538
that._isInstanceOf('Blob', file)) {
539+
var fileName = file.uploadName || file.name;
540+
if (options.uniqueFilenames) {
541+
fileName = that._getUniqueFilename(
542+
fileName,
543+
options.uniqueFilenames
544+
);
545+
}
513546
formData.append(
514547
($.type(options.paramName) === 'array' &&
515548
options.paramName[index]) || paramName,
516549
file,
517-
file.uploadName || file.name
550+
fileName
518551
);
519552
}
520553
});

0 commit comments

Comments
 (0)