2015-11-14 52 views
0

我正在使用ng-file-upload和multer将文件存储在uploads文件夹中,同时我也将文件名保存到数据库中,但当然不是在同一时刻。所以,如果我想保存原文件名,mul​​ter将做到这一点,如:ng-file-upload如何使用Upload.rename(文件,新名称)

filename: function (req, file, cb) { 
cb(null, file.originalname); 

我可以使用

cb(null, file.originalname + '-' + Date.now()); 

,以使该名称唯一,但随后在数据库中的文件名(从NG拍摄文件上传服务)是不同的。 我想在github/danialfarid/ng-file-upload页面上使用Upload.rename(file,newName),但是我所有尝试使用它都失败了。 这是NG-文件上传代码(第一部分)

$scope.uploadPic = function(files) { 
     for(var i = 0; i < $scope.files.length; i++) { 
      var $file = $scope.files[i]; 
      (function(index) { 
      $scope.upload[index] = Upload.upload({ 
       url: '/', 
       method: 'POST', 
       file: $file, 
      }).progress(function (evt) { 
       $scope.files[index].progress = Math.min(100, parseInt(100.0 * evt.loaded/evt.total)); 
      }).then(function (response) { 
       $timeout(function() { 
       $file.result = response.data; 

我已经试过var newName = $file.name + '-' + Date.now()

但后来我不确定如何应用Upload.rename(file, newName)

我想如果我设置新在multer获取它之前的名称,然后上传文件夹和数据库将具有相同的名称。 至少这是这个想法。谁能帮忙?

回答

0

我用NG-文件上传的旧版本(7.XX),它没有这个在NG-文件upload.js

this.rename = function (file, name) { 
    file.ngfName = name; 
    return file; 
    }; 

我更新到10.0.2版本 所以现在我可以使用Upload.rename($file, 'preview1.jpg'); 并且文件以新名称保存。

3
在上传选项

,设置文件密钥作为文件名,并使用相同的密钥为multer选项,例如:

,如果你有这样的为你的NG-文件上传选项:

$scope.upload[index] = Upload.upload({ 
      url: '/', 
      method: 'POST', 
      nameOfImage: $file, 
     }) 

为multer,你也应该有

upload.single('nameOfImage')

这个我试过用multer V1.2.0和NG-文件上传12.2.12

相关问题