Change filename when using express/multer

前端 未结 3 604
长发绾君心
长发绾君心 2021-02-10 11:04

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\',
           


        
3条回答
  •  别跟我提以往
    2021-02-10 11:58

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

提交回复
热议问题