Skip to content

Commit adf8b4a

Browse files
committed
#29 Added custom filename and content-type options to support identity-less streams.
1 parent b3a5d66 commit adf8b4a

4 files changed

Lines changed: 59 additions & 5 deletions

File tree

lib/form_data.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ FormData.prototype._multiPartHeader = function(field, value, options) {
118118
'Content-Disposition: form-data; name="' + field + '"';
119119

120120
// fs- and request- streams have path property
121+
// or use custom filename and/or contentType
121122
// TODO: Use request's response mime-type
122-
if (value.path) {
123+
if (options.filename || value.path) {
123124
header +=
124-
'; filename="' + path.basename(value.path) + '"' + FormData.LINE_BREAK +
125-
'Content-Type: ' + mime.lookup(value.path);
125+
'; filename="' + path.basename(options.filename || value.path) + '"' + FormData.LINE_BREAK +
126+
'Content-Type: ' + (options.contentType || mime.lookup(options.filename || value.path));
126127

127128
// http response has not
128129
} else if (value.readable && value.hasOwnProperty('httpVersion')) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Felix Geisendörfer <[email protected]> (http://debuggable.com/)",
33
"name": "form-data",
44
"description": "A module to create readable `\"multipart/form-data\"` streams. Can be used to submit forms and file uploads to other web applications.",
5-
"version": "0.0.7",
5+
"version": "0.0.8",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/felixge/node-form-data.git"
@@ -19,7 +19,7 @@
1919
"devDependencies": {
2020
"fake": "0.2.1",
2121
"far": "0.0.1",
22-
"formidable": "1.0.2",
22+
"formidable": "~1.0.13",
2323
"request": "~2.9.203"
2424
}
2525
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
test custom filename and content-type:
3+
re: https://github.com/felixge/node-form-data/issues/29
4+
*/
5+
6+
var common = require('../common');
7+
var assert = common.assert;
8+
var http = require('http');
9+
var fs = require('fs');
10+
11+
var FormData = require(common.dir.lib + '/form_data');
12+
var IncomingForm = require('formidable').IncomingForm;
13+
14+
var options = {
15+
filename: 'test.png',
16+
contentType: 'image/gif'
17+
};
18+
19+
var server = http.createServer(function(req, res) {
20+
21+
var form = new IncomingForm();
22+
form.uploadDir = common.dir.tmp;
23+
24+
form.parse(req);
25+
26+
form
27+
.on('file', function(name, file) {
28+
assert.strictEqual(name, 'my_file');
29+
assert.strictEqual(file.name, options.filename);
30+
assert.strictEqual(file.type, options.contentType);
31+
})
32+
.on('end', function() {
33+
res.writeHead(200);
34+
res.end('done');
35+
});
36+
});
37+
38+
39+
server.listen(common.port, function() {
40+
var form = new FormData();
41+
42+
form.append('my_file', fs.createReadStream(common.dir.fixture + '/unicycle.jpg'), options);
43+
44+
form.submit('http://localhost:' + common.port + '/', function(err, res) {
45+
if (err) {
46+
throw err;
47+
}
48+
49+
assert.strictEqual(res.statusCode, 200);
50+
server.close();
51+
});
52+
53+
});

test/tmp/.empty

Whitespace-only changes.

0 commit comments

Comments
 (0)