0

I'm using nodejs + multer + express the file is uploading but no response correct in the server side.

     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);
});
});

I receive in the server side:

{ 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 ...> } }

when i checked the destiny /var/www/new/upload/image nothing ther... what I'm doing wrong... i think the file upload ... but not storage the file...

4

1 回答 1

0

如果我错了,请纠正我你想在项目的特定文件夹中上传图像。为此使用多方视图并在你的路线中要求 multer 后写这样的帖子

`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` 
于 2015-07-22T19:52:48.823 回答