multer和nodejs上传文件

时间:2015-07-17 20:34:50

标签: node.js multer

我使用nodejs + multer + express文件正在上传,但在服务器端没有正确的响应。

     curl  -F 'file=@logo.png' http://mysite.de:8080/upload/image


    app.use(multer({ dest: '/var/www/likeyou-new/upload/image',
           limits: {
        fieldNameSize: 999999999,
        fieldSize: 999999999
      },
      includeEmptyFields: true,
      inMemory: true,
      onFileUploadStart: function(file) {
        console.log('Starting ' + file.fieldname);
      },
    //  onFileUploadData: function(file, data) {
    //    console.log('Got a chunk of data!');
    //  },

  onFileUploadComplete: function(file) {
    console.log('Completed file!');
  },
  onParseStart: function() {
    console.log('Starting to parse request!');
  },
  onParseEnd: function(req, next) {
    console.log('Done parsing!');
    next();
  },
  onError: function(e, next) {
    if (e) {
      console.log(e.stack);
    }
    next();
  }}));

    app.post('/upload/image', function(req, res,body){


    console.log(req.body) // form fields
    console.log(req.files) // form files
    res.status(204).end()
    var locate = '/var/www/likeyou-new/upload/image/'+"xx.png";
fs.writeFile(locate, req.files, function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});
});

我在服务器端收到:

{ file: 
   { fieldname: 'file',
     originalname: 'logo.png',
     name: '1042553d1dfb648ba305b39f184d28dc.png',
     encoding: '7bit',
     mimetype: 'application/octet-stream',
     path: '/var/www/new/upload/image/1042553d1dfb648ba305b39f184d28dc.png',
     extension: 'png',
     size: 820681,
     truncated: false,
     buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 01 2c 01 2c 00 00 ff ee 00 0e 41 64 6f 62 65 00 64 00 00 00 00 00 ff e1 10 b8 45 78 69 66 00 00 4d 4d 00 2a 00 ...> } }

当我检查命运/ var / www / new / upload / image没有什么......我做错了什么...我认为文件上传...但不存储文件...

1 个答案:

答案 0 :(得分:0)

纠正我,如果我错了你想要在项目的特定文件夹中上传图片。这样就可以在视图中使用多方并在你的路线中要求过滤后写这样的帖子

`router.post('/uploadimage',[ multer({ dest: './public/uploads/'}),function(req, res) {

  console.log(req.body) // form fields
  console.log(req.files) // form files
  res.render('uploadimage', { title:"Up load image page" });

}]);` 

和您的观点

`extends layout
block content
h1= title       form#formUploadImage(name="uploadimage",method="post",action="/uploadimage",enctype="multipart/form-data")
    input#inputUploadName(type="text", placeholder="uploadImage", name="uploadImage")
    input#inputImage(type="file", placeholder="image", name="image")
    button#btnSubmit(type="submit") submit`