Multer file.filename和file.path undefined

时间:2017-11-04 11:08:26

标签: javascript node.js express multer

我正在尝试使用express和multer上传/存储磁盘存储器上的图像。我的代码简述如下:

const express = require('express')
const multer = require('multer');
const upload = multer({ 
    destination: function(req, file, cb) {
        cb(null, 'uploads/')
    },
    filename: function(req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
app.post('/', upload.single('photo'), function(req, res) {
    console.log(req.file);
});

不幸的是,当我使用Postman发送POST请求(图像具有正确的字段名称“photo”)时,filename函数似乎不会影响名称。该文件最终进入/上传,但使用multer的默认命名方案(不是我在文件名功能中定义的自定义名称)。

此外,我的console.log(req.file)行输出:

{ fieldname: 'photo',
  originalname: 'test.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f ... >,
  size: 85404 }

请注意,req.file.filename中指定的req.file.destinationreq.file.pathvar v1 = ctx.Students.Where((Student s) => { return s.StudentName == "Bill"; }).FirstOrDefault<Student>(); var v2 = ctx.Students.Where((Student s) => s.StudentName == "Bill").FirstOrDefault<Student>(); var v3 = ctx.Students.Where(s => s.StudentName == "Bill").FirstOrDefault<Student>(); // You could skip the Where and do the filtering in FirstOrDefault directly var v4 = ctx.Students.FirstOrDefault<Student>(s => s.StudentName == "Bill"); var v5 = ctx.Students.FirstOrDefault(s => s.StudentName == "Bill"); 定义了值。

知道我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

好的,回答了我自己的问题。如果它对其他人有用,我的问题是:

我没有使用multer.diskStorage()来定义目标和文件名。如果要提供这些值,则需要使用df <- data.frame(x=c(0,0,0,4,4,4), layer.thickness = c(0.2,1.0,3.0,0.4,0.8,2.0), layer.depth = c(0.2,1.2,4.2,0.4,1.2,3.2), Petrography = c("silt", "gravel", "silt", "silt", "gravel", "sand"), BSCategory = c("Drilling1","Drilling1","Drilling1","Drilling2","Drilling2","Drilling2"), Offset = c(0,0,0,-1,-1,-1)) # provide a numeric ID that stops grouping individual petrography items df <- transform(df,ix=as.numeric(factor(df$BSCategory))); drilling <- ggplot(data = df, aes(x = x, y = layer.thickness, group = ix, fill = Petrography)) + theme_minimal() + theme(axis.line = element_line(colour = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.line.x = element_blank(), axis.ticks.y = element_line(), aspect.ratio=1) + geom_col(position = position_stack(reverse = TRUE), width= .15,color="black") + scale_y_reverse(expand = c(0, 0), name ="Depth [m]") + scale_x_continuous(position = "top", breaks = df$x, labels=paste(df$BSCategory,"\n",df$x,"m"), name="") + scale_fill_manual(values = c("gravel"='#f3e03a', "sand"= '#e09637', "silt"='#aba77d')) print(drilling) 方法。正确的用法是:

.diskStorage()

然后你可以正常使用multer作为中间件,例如:

const multer = require('multer');
const storage = multer.diskStorage({ // notice you are calling the multer.diskStorage() method here, not multer()
    destination: function(req, file, cb) {
        cb(null, 'uploads/')
    },
    filename: function(req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
const upload = multer({storage}); //provide the return value from