简体   繁体   中英

multer and nodejs upload file

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

Correct me if i am wrong you want upload image in specific folder of your project.for this use multiparty in view and after requiring multer in your route write post like this

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

}]);` 

and your view

`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` 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM