Skip to content

Commit 9512d0c

Browse files
committed
Filename can be a nested path
1 parent d7398c3 commit 9512d0c

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

Readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ someModule.stream(function(err, stdout, stderr) {
132132
var form = new FormData();
133133

134134
form.append('file', stdout, {
135-
filename: 'unicycle.jpg',
135+
filename: 'unicycle.jpg', // ... or:
136+
filepath: 'photos/toys/unicycle.jpg',
136137
contentType: 'image/jpg',
137138
knownLength: 19806
138139
});
@@ -144,6 +145,8 @@ someModule.stream(function(err, stdout, stderr) {
144145
});
145146
```
146147

148+
The `filepath` property overrides `filename` and may contain a relative path. This is typically used when uploading [multiple files from a directory](https://wicg.github.io/entries-api/#dom-htmlinputelement-webkitdirectory).
149+
147150
For edge cases, like POST request to URL with query string or to pass HTTP auth credentials, object can be passed to `form.submit()` as first parameter:
148151

149152
``` javascript

lib/form_data.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,25 @@ FormData.prototype._multiPartHeader = function(field, value, options) {
209209

210210
FormData.prototype._getContentDisposition = function(value, options) {
211211

212-
var contentDisposition;
213-
214-
// custom filename takes precedence
215-
// fs- and request- streams have path property
216-
// formidable and the browser add a name property.
217-
var filename = options.filename || value.name || value.path;
212+
var filename
213+
, contentDisposition
214+
;
218215

219-
// or try http response
220-
if (!filename && value.readable && value.hasOwnProperty('httpVersion')) {
221-
filename = value.client._httpMessage.path;
216+
if (typeof options.filepath === 'string') {
217+
// custom filepath for relative paths
218+
filename = path.normalize(options.filepath).replace(/\\/g, '/');
219+
} else if (options.filename || value.name || value.path) {
220+
// custom filename take precedence
221+
// formidable and the browser add a name property
222+
// fs- and request- streams have path property
223+
filename = path.basename(options.filename || value.name || value.path);
224+
} else if (value.readable && value.hasOwnProperty('httpVersion')) {
225+
// or try http response
226+
filename = path.basename(value.client._httpMessage.path);
222227
}
223228

224229
if (filename) {
225-
contentDisposition = 'filename="' + path.basename(filename) + '"';
230+
contentDisposition = 'filename="' + filename + '"';
226231
}
227232

228233
return contentDisposition;
@@ -248,9 +253,9 @@ FormData.prototype._getContentType = function(value, options) {
248253
contentType = value.headers['content-type'];
249254
}
250255

251-
// or guess it from the filename
252-
if (!contentType && options.filename) {
253-
contentType = mime.lookup(options.filename);
256+
// or guess it from the filepath or filename
257+
if (!contentType && (options.filepath || options.filename)) {
258+
contentType = mime.lookup(options.filepath || options.filename);
254259
}
255260

256261
// fallback to the default content type if `value` is not simple value

test/integration/test-custom-filename.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ var assert = common.assert;
88
var mime = require('mime-types');
99
var http = require('http');
1010
var fs = require('fs');
11+
var path = require('path');
1112

1213
var FormData = require(common.dir.lib + '/form_data');
1314
var IncomingForm = require('formidable').IncomingForm;
1415

15-
var knownFile = common.dir.fixture + '/unicycle.jpg';
16-
var unknownFile = common.dir.fixture + '/unknown_file_type';
16+
var knownFile = path.join(common.dir.fixture, 'unicycle.jpg');
17+
var unknownFile = path.join(common.dir.fixture, 'unknown_file_type');
18+
var relativeFile = path.relative(path.join(knownFile, '..', '..'), knownFile);
1719

1820
var options = {
1921
filename: 'test.png',
@@ -35,6 +37,10 @@ var server = http.createServer(function(req, res) {
3537
assert.strictEqual(files['custom_filename'].name, options.filename, 'Expects custom filename');
3638
assert.strictEqual(files['custom_filename'].type, mime.lookup(knownFile), 'Expects original content-type');
3739

40+
assert('custom_filepath' in files);
41+
assert.strictEqual(files['custom_filepath'].name, relativeFile.replace(/\\/g, '/'), 'Expects custom filename');
42+
assert.strictEqual(files['custom_filepath'].type, mime.lookup(knownFile), 'Expects original content-type');
43+
3844
assert('unknown_with_filename' in files);
3945
assert.strictEqual(files['unknown_with_filename'].name, options.filename, 'Expects custom filename');
4046
assert.strictEqual(files['unknown_with_filename'].type, mime.lookup(options.filename), 'Expects filename-derived content-type');
@@ -67,6 +73,8 @@ server.listen(common.port, function() {
6773
form.append('unknown_with_filename', fs.createReadStream(unknownFile), options.filename);
6874
// Filename only with unknown file
6975
form.append('unknown_with_filename_as_object', fs.createReadStream(unknownFile), {filename: options.filename});
76+
// Filename with relative path
77+
form.append('custom_filepath', fs.createReadStream(knownFile), {filepath: relativeFile});
7078
// No options or implicit file type from extension on name property.
7179
var customNameStream = fs.createReadStream(unknownFile);
7280
customNameStream.name = options.filename;

0 commit comments

Comments
 (0)