使用express / multer时更改文件名

时间:2015-12-02 05:24:19

标签: node.js express multer

我在上传pdf时遇到问题,当我上传它时(我正在使用ng-file-upload),达到express.js的文件json是:

{ 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 }

我想将名称更改为我从angular发送到express的属性,这里是所有设置:

html元素:

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

快递:

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

}

3 个答案:

答案 0 :(得分:4)

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

}

答案 1 :(得分:0)

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads/')
  },
  filename: function (req, file, cb) {

      cb(null,  file.originalname );

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

在快递中配置如下

答案 2 :(得分:0)

在Angular中,您需要将 new_file_name 移动到数据中的文件上方,如下所示:

select TID,`TP Num`,REF from (
  select TID,REF,@rn:= if (@prev_tid = TID,@rn+1,1) as `TP Num`,@prev_tid:=TID  from
  (
    select
    TID,REF from
    (
        select TID , TP1 as REF from mytable where TP1 is not null and TP1 <> ''
        union all 
        select TID , TP2 as REF from mytable where TP2 is not null and TP2 <> ''
        union all
        select TID , TP3 as REF from mytable where TP3 is not null and TP3 <> ''
    )x
  )y,(select @rn:=0,@prev_tid:=null)z
  order by TID,REF 
)x;

基本上浏览器需要在文件之前发送数据...可以在此处找到更好的解释:https://github.com/expressjs/multer/issues/134