ドキュメントは、あなたはおそらく、アップロードするファイルをスキップするのFileFilterを使用する必要があります状態。
のFileFilter(https://github.com/expressjs/multer#filefilter)
Set this to a function to control which files should be uploaded and which should be skipped. The function should look like this:
function fileFilter (req, file, cb) {
// The function should call `cb` with a boolean
// to indicate if the file should be accepted
// To reject this file pass `false`, like so:
cb(null, false)
// To accept the file pass `true`, like so:
cb(null, true)
// You can always pass an error if something goes wrong:
cb(new Error('I don\'t have a clue!'))
}
私はfile
に渡されたプロパティmimetype
(https://github.com/expressjs/multer#api)を持っていることを前提としていますドキュメントから。これは、スキップしたい場合は、決定のヒントになる可能性があります。
編集: このGHの問題(https://github.com/expressjs/multer/issues/114#issuecomment-231591339)には、使用例があります。これはファイルの拡張子を見るだけでなく、簡単に名前を変更することができるだけでなく、MIMEタイプも考慮に入れておくことが重要です。ヒントの
const path = require('path');
multer({
fileFilter: function (req, file, cb) {
var filetypes = /jpeg|jpg/;
var mimetype = filetypes.test(file.mimetype);
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
}
cb("Error: File upload only supports the following filetypes - " + filetypes);
}
});
HTH
ありがとう!しかし、それは私の場合に関連する例で完全な答えではありません:) – Coder1000
これは非常によく関連すると思います。リンクされたgithubの問題は、同じ関数(fileFilter)を使用します。私はまた、ファイル名の拡張子が簡単に間違っている可能性があるので、mime-typeをチェックすることを好むでしょう。投票を理解していない。余分なif節は問題ではないと思った。 https://github.com/expressjs/multer/issues/114#issuecomment-231591339 :-) – silverfighter
あなたはあなたの答えにこれを含めていませんでした:/あなたの答えを編集するのはどうですか? :) – Coder1000