Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Key | Description
`fileFilter` | Function to control which files are accepted
`limits` | Limits of the uploaded data
`preservePath` | Keep the full path of files instead of just the base name
`charset` | For multipart forms, the default character set to use for values of part header parameters (e.g. filename) that are not extended parameters (that contain an explicit charset). **Default**: `'latin1'`.

In an average web app, only `dest` might be required, and configured as shown in
the following example.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function Multer (options) {

this.limits = options.limits
this.preservePath = options.preservePath
// TODO: next major version change default to utf-8
this.charset = options.charset || 'latin1'
this.fileFilter = options.fileFilter || allowAll
}

Expand Down Expand Up @@ -47,6 +49,7 @@ Multer.prototype._makeMiddleware = function (fields, fileStrategy) {
return {
limits: this.limits,
preservePath: this.preservePath,
charset: this.charset,
storage: this.storage,
fileFilter: wrappedFileFilter,
fileStrategy: fileStrategy
Expand Down Expand Up @@ -77,6 +80,7 @@ Multer.prototype.any = function () {
return {
limits: this.limits,
preservePath: this.preservePath,
charset: this.charset,
storage: this.storage,
fileFilter: this.fileFilter,
fileStrategy: 'ARRAY'
Expand Down
8 changes: 7 additions & 1 deletion lib/make-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function makeMiddleware (setup) {
var fileFilter = options.fileFilter
var fileStrategy = options.fileStrategy
var preservePath = options.preservePath
var defParamCharset = options.charset

req.body = Object.create(null)

Expand All @@ -35,7 +36,12 @@ function makeMiddleware (setup) {
var busboy

try {
busboy = Busboy({ headers: req.headers, limits: limits, preservePath: preservePath })
busboy = Busboy({
headers: req.headers,
limits,
preservePath,
defParamCharset
})
} catch (err) {
return next(err)
}
Expand Down
55 changes: 55 additions & 0 deletions test/unicode.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,58 @@ describe('Unicode', function () {
})
})
})

describe('UTF-8 filenames', function () {
var uploadDir, upload

beforeEach(function (done) {
temp.mkdir(function (err, path) {
if (err) return done(err)

var storage = multer.diskStorage({
destination: path,
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})

uploadDir = path
upload = multer({ storage: storage, charset: 'utf-8' })

done()
})
})

afterEach(function (done) {
rimraf(uploadDir, done)
})

it('should handle UTF-8 filenames', function (done) {
var req = new stream.PassThrough()
var boundary = 'AaB03x'
var body = [
'--' + boundary,
'Content-Disposition: form-data; name="small0"; filename="ÖoϪ.dat"',
Copy link
Member

@ShubhamOulkar ShubhamOulkar Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

   filename="ÖoϪ.dat"',

see #1210 (comment)

'Content-Type: text/plain',
'',
'test with UTF-8 filename',
'--' + boundary + '--'
].join('\r\n')

req.headers = {
'content-type': 'multipart/form-data; boundary=' + boundary,
'content-length': body.length
}

req.end(body)

upload.single('small0')(req, null, function (err) {
assert.ifError(err)

assert.strictEqual(req.file.originalname, 'ÖoϪ.dat')
assert.strictEqual(req.file.fieldname, 'small0')

done()
})
})
})