File upload callback with Multer - NodeJS

5

Hello, has anyone used the multer (express / nodeJS module) to perform file uploads? If yes how did you catch the callback events (onFileUploadStart and onFileUploadComplete)? Theoretically I'm using correctly but at no time are events called ...

Sample code:

'use strict';
//DEFINO AS DEPENDENCIAS
var express = require('express');
var multer = require('multer');
var uploadRealizado = false;
var servidor = express();


//CONFIGURO O MULTER NA INSTANCIA DO EXPRESS(no caso nosso servidor)
var upload = multer({
    dest: './testeUpload/',
    rename: function(nomeCampo, nomeArquivo) {
        return nomeArquivo+Date.now();
    },
    onFileUploadStart: function(arquivo) {
        console.log('COMEÇOU O UPLOAD');        
    },
    onFileUploadComplete: function (arquivo) {
        console.log('TERMINOU O UPLOAD');       
        uploadRealizado = true;
    }
});

//APLICANDO AS ROTAS
servidor.get('/', function(requisicao, resposta) {
    resposta.sendfile('./home.html');
});

servidor.post('/api/photo', upload.single('avatar'), function(requisicao, resposta) {
    console.log('STATUS UPLOAD' + uploadRealizado);
    console.log('ARQUIVOS', requisicao.file | requisicao.files);
    if (uploadRealizado) {              
        resposta.send('foto enviada');
    }
    resposta.send('foto não enviada');
});


//INICIO O SERVIDOR
servidor.listen(3000, function(){
    console.log('servidor rodando na porta 3000');
});

Att,

    
asked by anonymous 27.07.2015 / 22:16

1 answer

2

Look, I'll try to help by just translating this response here .

It seems that usage has changed over time. Currently, multer builder only accepts following options ( link ):

dest or storage - Where to store the files  fileFilter - Function to control which files are accepted  limits - Limits of data sent So, for example, the name change should be resolved through the appropriate storage configuration ( link ).

var storage = multer.diskStorage ({destination: function (req, file, cb) {cb (null, '/ tmp / my-uploads'); // Absolute path. () {}}, varen = multer ({storage: storage}); app.php ('/ profile', upload.single ('fieldname'), function (req, res, next) {// req.body contains the text fields}); The fieldname must match the name of the field in the body of the request. That is, in the case of HTML form post, the name of the input form upload element.

Also take a look at other middleware functions such as array and fields - link that provide aa few functionality.

In addition, you may be interested in limits ( link ) and file filter ( link )

And also - source is the only source of truth - have a peek ( link )

    
14.09.2015 / 13:40