Skip to content

Commit f8d2cc3

Browse files
Ersun WarnckeSeinzu
authored andcommitted
allow file upload without filename
1 parent e5db9ca commit f8d2cc3

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

lib/make-middleware.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ function makeMiddleware (setup) {
112112

113113
if (fieldname == null) return abortWithCode('MISSING_FIELD_NAME')
114114

115-
// don't attach to the files object, if there is no file
116-
if (!filename) return fileStream.resume()
115+
// filename is not required (https://tools.ietf.org/html/rfc1867) but if
116+
// filename not present busboy only treats as file if content type is
117+
// application/octet-stream
118+
if (!filename) filename = 'undefined'
117119

118120
// Work around bug in Busboy (https://github.com/mscdex/busboy/issues/6)
119121
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNameSize')) {

test/files/no-filename.dat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--99999
2+
Content-Disposition: form-data; name="textField"
3+
Content-Type: text/plain; charset=ISO-8859-1
4+
5+
foo
6+
--99999
7+
Content-Disposition: form-data; name="fileField"
8+
Content-Type: application/octet-stream
9+
10+
foo
11+
--99999--

test/no-filename.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-env mocha */
2+
3+
var assert = require('assert')
4+
var fs = require('fs')
5+
var onFinished = require('on-finished')
6+
var path = require('path')
7+
8+
var multer = require('../')
9+
10+
describe('File with no filename', function () {
11+
var upload
12+
13+
before(function () {
14+
upload = multer()
15+
})
16+
17+
it('should accept file without filename', function (done) {
18+
var parser = upload.any()
19+
20+
var filePath = path.join(__dirname, 'files', 'no-filename.dat')
21+
var req = fs.createReadStream(filePath)
22+
req.headers = {
23+
'content-type': 'multipart/form-data; boundary=99999',
24+
'content-length': fs.statSync(filePath).size
25+
}
26+
27+
parser(req, null, function (err) {
28+
onFinished(req, function () {
29+
assert.ifError(err)
30+
assert.equal(req.files.length, 1)
31+
assert.equal(req.files[0].fieldname, 'fileField')
32+
assert.equal(req.files[0].buffer.toString(), 'foo')
33+
done()
34+
})
35+
})
36+
})
37+
})

0 commit comments

Comments
 (0)