我正在使用ng-file-upload和multer將文件存儲在uploads文件夾中,同時我也將文件名保存到數據庫中,但當然不是在同一時刻。所以,如果我想保存原文件名,multer將做到這一點,如: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獲取它之前的名稱,然後上傳文件夾和數據庫將具有相同的名稱。 至少這是這個想法。誰能幫忙?