Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change filename when using express/multer

I'm having trouble uploading a pdf, when I upload it (I'm using ng-file-upload) the file json that reachs express.js is:

{ fieldname: 'file',
  originalname: 'db.pdf',
  encoding: '7bit',
  mimetype: 'application/pdf',
  destination: './client/public/docs/transactions/',
  filename: '8a3fe7cb7369d9cffa8e17b162ec3d6b',
  path: 'client\\public\\docs\\transactions\\8a3fe7cb7369d9cffa8e17b162ec3d6b',
  size: 9179145 }

I'd like to change the name to a property that I send from angular to express, here's all the setup:

html element:

<div ngf-select="vm.uploadPdf($file, $errFiles)"
     ngf-pattern="'.pdf'"
     accept="application/pdf" ngf-max-size="20MB">Select ...
</div>

angular func uploadPdf (ctrl as vm):

function uploadPdf( file, errFiles ) {
    vm.pdf = file;
    vm.errorFile = errFiles && errFiles[ 0 ];

    if ( vm.pdf ) {
        Upload
            .upload( {
                url: '/upload',
                data: {
                    file: vm.pdf,
                    new_file_name: vm.new_name//this is the filename I want
                }
            } )
            .then(
                function ( response ) {
                    $timeout( function () {
                        vm.pdf.result = response.data;
                    } );
                }, function ( response ) {
                    if ( response.status > 0 ) {
                        vm.errorQuedanPdfUpload = response.status + ': ' + response.data;
                        console.log( vm.errorQuedanPdfUpload );
                    }
                }
            );
    }
}

express:

var express = require( 'express' ),
    router  = express.Router(),
    util    = require( 'util' ),
    multer  = require( 'multer' );

var storage = multer.diskStorage(
    {
        destination: './uploads/',
        filename: function ( req, file, cb ) {
            //req.body is empty...
            //How could I get the new_file_name property sent from client here?
            cb( null, file.originalname );
        }
    }
);

var upload = multer( { storage: storage } );

router.route( '/upload' )
    .post( upload.single( 'file' ), post );

function post( request, response ) {

    response.json( { message: 'Files Uploaded!' } );

}
like image 886
Jonathan Solorzano Avatar asked Dec 02 '15 05:12

Jonathan Solorzano


People also ask

Can I use multer without Express?

You cannot use Multer without because it's Express middleware.

What does multer do in Express?

Usage. Multer adds a body object and a file or files object to the request object. The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form.

How do I rename a file in node js?

Node FS Rename File – To rename file with Node FS, use fs. rename(new_file_name, old_file_name, callback_function) for asynchronous file rename operation and use fs. renameSync(new_file_name, old_file_name) for synchronous file rename operation.


1 Answers

var express = require( 'express' ),
    router  = express.Router(),
    util    = require( 'util' ),
    multer  = require( 'multer' );

var storage = multer.diskStorage(
    {
        destination: './uploads/',
        filename: function ( req, file, cb ) {
            //req.body is empty...
            //How could I get the new_file_name property sent from client here?
            cb( null, file.originalname+ '-' + Date.now()+".pdf");
        }
    }
);

var upload = multer( { storage: storage } );

router.route( '/upload' )
    .post( upload.single( 'file' ), post );

function post( request, response ) {

    response.json( { message: 'Files Uploaded!' } );

}
like image 53
Pradeep Saini Avatar answered Sep 18 '22 04:09

Pradeep Saini