Multer file upload callback - Nodejs

Asked

Viewed 711 times

5

Hello anyone ever used multer(express/nodejs module) to upload files? If so, how did you manage to capture the events of callbacks (onFileUploadStart and onFileUploadComplete)? Theoretically I am 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,

  • I don’t see those callbacks in the documentation you can point out where you saw that the woman has callbacks?

  • so... I saw in almost every tutorial involving Multer... : https://codeforgeek.com/2014/11/file-uploads-using-node-js/ http://stackoverflow.com/questions/26702869/multer-not-uploading-files-when-i-include-logic-onfileuploadstartnodejs *As documentation is not always a strong of nodejs modules I ended up going with most

1 answer

2


Look, I’ll try to help just by translating that answer here.

It seems that the usage has changed over time. Currently, multer constructor only accepts following options (https://www.npmjs.com/package/multer#multer-opts):

dest or Storage - Where to store the files fileFilter - Function to control which files are accepted Limits - Data limits sent Thus, for example the name change should be solved through the appropriate storage configuration (https://www.npmjs.com/package/multer#Storage).

var Storage = multer.diskStorage({ Destination: Function (req, file, cb) { cb(null, '/tmp/my-uploads'); // Absolute path. Folder must exist, will not be created for you. }, filename: Function (req, file, cb) { cb(null, file.fieldname + '-' + Date.now()); } }) var upload = multer({ Storage: Storage }); app.post('/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 post form, the entry name element upload form.

Also take a look at other middleware functions like array and Fields - https://www.npmjs.com/package/multer#single-fieldname that provide a little different functionality.

Also, you may be interested in the limits (https://www.npmjs.com/package/multer#Limits) and file filter (https://www.npmjs.com/package/multer#filefilter)

And also - source is the only source of truth - have a peek (https://github.com/expressjs/multer/blob/master/index.js)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.