Skip to content

Commit 04f9746

Browse files
committed
Filename can be a nested path
1 parent d7398c3 commit 04f9746

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/form_data.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,21 @@ FormData.prototype._getContentDisposition = function(value, options) {
211211

212212
var contentDisposition;
213213

214-
// custom filename takes precedence
214+
// custom filepath or filename take precedence
215215
// fs- and request- streams have path property
216216
// formidable and the browser add a name property.
217-
var filename = options.filename || value.name || value.path;
217+
var filename = options.filepath || options.filename || value.name || value.path;
218218

219219
// or try http response
220220
if (!filename && value.readable && value.hasOwnProperty('httpVersion')) {
221221
filename = value.client._httpMessage.path;
222222
}
223223

224224
if (filename) {
225-
contentDisposition = 'filename="' + path.basename(filename) + '"';
225+
if (!options.filepath) {
226+
filename = path.basename(filename);
227+
}
228+
contentDisposition = 'filename="' + filename + '"';
226229
}
227230

228231
return contentDisposition;

test/integration/test-custom-filename.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ var server = http.createServer(function(req, res) {
3535
assert.strictEqual(files['custom_filename'].name, options.filename, 'Expects custom filename');
3636
assert.strictEqual(files['custom_filename'].type, mime.lookup(knownFile), 'Expects original content-type');
3737

38+
assert('custom_filepath' in files);
39+
assert.strictEqual(files['custom_filepath'].name, common.dir.fixture + '/' + options.filename, 'Expects custom filename');
40+
assert.strictEqual(files['custom_filepath'].type, mime.lookup(knownFile), 'Expects original content-type');
41+
3842
assert('unknown_with_filename' in files);
3943
assert.strictEqual(files['unknown_with_filename'].name, options.filename, 'Expects custom filename');
4044
assert.strictEqual(files['unknown_with_filename'].type, mime.lookup(options.filename), 'Expects filename-derived content-type');
@@ -67,6 +71,8 @@ server.listen(common.port, function() {
6771
form.append('unknown_with_filename', fs.createReadStream(unknownFile), options.filename);
6872
// Filename only with unknown file
6973
form.append('unknown_with_filename_as_object', fs.createReadStream(unknownFile), {filename: options.filename});
74+
// Filename with nested path
75+
form.append('custom_filepath', fs.createReadStream(knownFile), {filepath: common.dir.fixture + '/' + options.filename});
7076
// No options or implicit file type from extension on name property.
7177
var customNameStream = fs.createReadStream(unknownFile);
7278
customNameStream.name = options.filename;

0 commit comments

Comments
 (0)