ng-file-upload 如何使用 Upload.rename(file, newName)
ng-file-upload How to use Upload.rename(file, newName)
我正在使用 ng-file-upload 和 multer 将文件存储在上传文件夹中,我还将文件名保存到数据库中,但当然不是同时保存。所以如果我想保存原始文件名,multer 会这样做:
filename: function (req, file, cb) {
cb(null, file.originalname);
我可以用
cb(null, file.originalname + '-' + Date.now());
使名称唯一,但数据库中的文件名(取自 ng-file-upload 服务)不同。
我想像在 github/danialfarid/ng-file-upload 页面上那样使用 Upload.rename(file, newName) 但我所有使用它的尝试都失败了。
这是 ng-file-upload 代码(第一部分)
$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 获取它之前设置新名称,那么上传文件夹和数据库将具有相同的名称。
至少那是想法。有人可以帮忙吗?
我使用的是旧版本的 ng-file-upload (7.X.X),它在 ng-file-upload.js
中没有这个
this.rename = function (file, name) {
file.ngfName = name;
return file;
};
我更新到10.0.2版本
所以现在我可以使用 Upload.rename($file, 'preview1.jpg');
文件以新名称保存。
在上传选项中,将文件密钥设置为文件名,并为多个选项使用相同的密钥,例如:
如果您的 ng-file-upload 选项有这个:
$scope.upload[index] = Upload.upload({
url: '/',
method: 'POST',
nameOfImage: $file,
})
对于 multer,你还应该有
upload.single('nameOfImage')
我尝试使用 multer v1.2.0 和 ng-file-upload 12.2.12
我正在使用 ng-file-upload 和 multer 将文件存储在上传文件夹中,我还将文件名保存到数据库中,但当然不是同时保存。所以如果我想保存原始文件名,multer 会这样做:
filename: function (req, file, cb) {
cb(null, file.originalname);
我可以用
cb(null, file.originalname + '-' + Date.now());
使名称唯一,但数据库中的文件名(取自 ng-file-upload 服务)不同。 我想像在 github/danialfarid/ng-file-upload 页面上那样使用 Upload.rename(file, newName) 但我所有使用它的尝试都失败了。 这是 ng-file-upload 代码(第一部分)
$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 获取它之前设置新名称,那么上传文件夹和数据库将具有相同的名称。 至少那是想法。有人可以帮忙吗?
我使用的是旧版本的 ng-file-upload (7.X.X),它在 ng-file-upload.js
中没有这个this.rename = function (file, name) {
file.ngfName = name;
return file;
};
我更新到10.0.2版本
所以现在我可以使用 Upload.rename($file, 'preview1.jpg');
文件以新名称保存。
在上传选项中,将文件密钥设置为文件名,并为多个选项使用相同的密钥,例如:
如果您的 ng-file-upload 选项有这个:
$scope.upload[index] = Upload.upload({
url: '/',
method: 'POST',
nameOfImage: $file,
})
对于 multer,你还应该有
upload.single('nameOfImage')
我尝试使用 multer v1.2.0 和 ng-file-upload 12.2.12