如何使用multer存储具有文件扩展名的文件? [英] How to store a file with file extension with multer?

查看:125
本文介绍了如何使用multer存储具有文件扩展名的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设法将我的文件存储在一个文件夹中,但它们存储时没有文件扩展名.

Managed to store my files in a folder but they store without the file extension.

有谁知道我将如何存储带有文件扩展名的文件?

Does any one know how would I store the file with file extension?

推荐答案

来自文档:Multer 不会为您附加任何文件扩展名,您的函数应该返回一个带有文件扩展名的文件名."

From the docs: "Multer will not append any file extension for you, your function should return a filename complete with an file extension."

以下是添加扩展程序的方法:

Here's how you can add the extension:

var multer = require('multer');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + '.jpg') //Appending .jpg
  }
})

var upload = multer({ storage: storage });

我建议使用 mimetype 属性来确定扩展名.例如:

I would recommend using the mimetype property to determine the extension. For example:

filename: function (req, file, cb) {
  console.log(file.mimetype); //Will return something like: image/jpeg

更多信息:https://github.com/expressjs/multer

这篇关于如何使用multer存储具有文件扩展名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆