admin管理员组文章数量:1287584
const diskStorage = multer.diskStorage({
destination: 'uploads/',
filename: (req, file, cb) => {
cb(null, Date.now() + file.originalname);
}
});
why we cant assign file name directly ?
const diskStorage = multer.diskStorage({
destination: 'uploads/',
filename: "myfile.png" // ❌ This gives an error
});
const diskStorage = multer.diskStorage({
destination: 'uploads/',
filename: (req, file, cb) => {
cb(null, Date.now() + file.originalname);
}
});
why we cant assign file name directly ?
const diskStorage = multer.diskStorage({
destination: 'uploads/',
filename: "myfile.png" // ❌ This gives an error
});
Share
Improve this question
edited Feb 23 at 14:46
Karl-Johan Sjögren
17.6k7 gold badges63 silver badges72 bronze badges
asked Feb 23 at 14:42
Naga mani kanta manamNaga mani kanta manam
394 bronze badges
1
- 1 This configures the engine — it’s unlikely you’d want the engine to overwrite the file on each request (you could, of course, with a one-liner that returns a string, barely longer than the string itself). – Dave Newton Commented Feb 23 at 14:58
2 Answers
Reset to default 2Multer needs filename
as a function cuz filenames should be dynamic, not fixed. If you set it as "myfile.png"
, every upload will overwrite the last one. The function lets you use file.originalname
, timestamps, etc., and ensures async handling with cb()
.
So yeah, it's just how Multer works to avoid conflicts and allow flexibility!
本文标签:
版权声明:本文标题:javascript - Why does multer.diskStorage require a function for filename instead of a direct string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741315000a2371863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论