Skip to content

Commit e622bd8

Browse files
committed
Add support for relative path for file attachments
1 parent ddb3762 commit e622bd8

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

Readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ someModule.stream(function(err, stdout, stderr) {
145145
});
146146
```
147147

148+
By default only the basename of the file is included in the form. To include the full path, pass `includePath: true`.
149+
150+
``` javascript
151+
form.append('file', stream, {
152+
filename: 'images/logo.png',
153+
includePath: true
154+
});
155+
```
156+
148157
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:
149158

150159
``` javascript

lib/form_data.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ FormData.prototype._getContentDisposition = function(value, options) {
200200
}
201201

202202
if (filename) {
203-
contentDisposition = 'filename="' + path.basename(filename) + '"';
203+
var formFilename = options.includePath ? filename : path.basename(filename);
204+
contentDisposition = 'filename="' + formFilename + '"';
204205
}
205206

206207
return contentDisposition;

test/integration/test-custom-filename.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ var options = {
2020
contentType: 'image/gif'
2121
};
2222

23+
var optionsWithPath = {
24+
filename: 'subdir/test.png',
25+
};
26+
2327
var server = http.createServer(function(req, res) {
2428

2529
var form = new IncomingForm({uploadDir: common.dir.tmp});
@@ -43,6 +47,9 @@ var server = http.createServer(function(req, res) {
4347
assert.strictEqual(files['unknown_with_filename_as_object'].name, options.filename, 'Expects custom filename');
4448
assert.strictEqual(files['unknown_with_filename_as_object'].type, mime.lookup(options.filename), 'Expects filename-derived content-type');
4549

50+
assert('relative_path' in files);
51+
assert.strictEqual(files['relative_path'].name, optionsWithPath.filename, 'Expects filename with relative path');
52+
4653
assert('unknown_everything' in files);
4754
assert.strictEqual(files['unknown_everything'].type, FormData.DEFAULT_CONTENT_TYPE, 'Expects default content-type');
4855

@@ -63,6 +70,8 @@ server.listen(common.port, function() {
6370
form.append('unknown_with_filename', fs.createReadStream(unknownFile), options.filename);
6471
// Filename only with unknown file
6572
form.append('unknown_with_filename_as_object', fs.createReadStream(unknownFile), {filename: options.filename});
73+
// Filename with a relative path
74+
form.append('relative_path', fs.createReadStream(unknownFile), {filename: optionsWithPath.filename, includePath: true});
6675
// No options or implicit file type from extension.
6776
form.append('unknown_everything', fs.createReadStream(unknownFile));
6877

0 commit comments

Comments
 (0)