如何在 Angular 文件管理器中检测上传时存在的文件名
How to detect file name exist on upload in Angular file manager
我在我当前的 Angularjs 应用程序中使用 Angular File Manager。
我想在上传目录中存在名称的文件时向用户显示错误(例如:文件名存在)。
$scope.uploadFiles = function () {
/******* I added these ↓↓↓ ********/
var item = $scope.singleSelection();
if (item) {
var name = item.tempModel.name.trim();
var nameExists = $scope.fileNavigator.fileNameExists(name);
if (nameExists && validateSamePath(item)) {
// this name is exist, so ↓
$scope.apiMiddleware.apiHandler.error = $translate.instant('error_invalid_filename');
return false;
}
}
/******* these code are exist before(your code) ↓↓↓ ********/
$scope.apiMiddleware.upload($scope.uploadFileList, $scope.fileNavigator.currentPath).then(function () {
$scope.fileNavigator.refresh();
$scope.uploadFileList = [];
$scope.modal('uploadfile', true);
}, function (data) {
var errorMsg = data.result && data.result.error || $translate.instant('error_uploading_files');
$scope.apiMiddleware.apiHandler.error = errorMsg;
});
}
但在这种情况下,我应该 select 一个项目然后上传一个文件,这意味着,如果我不 select 一个文件并上传一个同名的新文件,错误没有出现。
我需要检测上传的当前文件 fileName
我怎样才能做到这一点?
提前致谢
经过多次搜索和测试代码,我解决了这个问题。
我在文件名中添加时间戳如果重复。
在main.js文件中
function checkRepetitive(file) {
var currentList = $scope.fileNavigator.fileList;
var repetitive = false;
angular.forEach(currentList, function (item, index) {
if (item.model.name === file.name) {
repetitive = true
}
});
return repetitive;
}
// click on upload file button in upload modal
$scope.uploadFiles = function () {
angular.forEach($scope.uploadFileList, function (item, index) {
if (checkRepetitive(item)) {
// `morteza.jpg` => `morteza` + `new Date().getTime()).toString()` + `.jpg`
var lastDotIndex = item.name.lastIndexOf('.');
var fileName = item.name.substring(0, lastDotIndex);
var extension = item.name.substring(lastDotIndex, item.name.length);
// name was `read only` so, I can't change that if didn't write this ↓
Object.defineProperty(item, 'name', {
writable: true
});
angular.extend(item, {'name': fileName + (new Date().getTime()).toString() + extension});
} // if (checkRepetitive(item)) {
});
我在我当前的 Angularjs 应用程序中使用 Angular File Manager。
我想在上传目录中存在名称的文件时向用户显示错误(例如:文件名存在)。
$scope.uploadFiles = function () {
/******* I added these ↓↓↓ ********/
var item = $scope.singleSelection();
if (item) {
var name = item.tempModel.name.trim();
var nameExists = $scope.fileNavigator.fileNameExists(name);
if (nameExists && validateSamePath(item)) {
// this name is exist, so ↓
$scope.apiMiddleware.apiHandler.error = $translate.instant('error_invalid_filename');
return false;
}
}
/******* these code are exist before(your code) ↓↓↓ ********/
$scope.apiMiddleware.upload($scope.uploadFileList, $scope.fileNavigator.currentPath).then(function () {
$scope.fileNavigator.refresh();
$scope.uploadFileList = [];
$scope.modal('uploadfile', true);
}, function (data) {
var errorMsg = data.result && data.result.error || $translate.instant('error_uploading_files');
$scope.apiMiddleware.apiHandler.error = errorMsg;
});
}
但在这种情况下,我应该 select 一个项目然后上传一个文件,这意味着,如果我不 select 一个文件并上传一个同名的新文件,错误没有出现。
我需要检测上传的当前文件 fileName
我怎样才能做到这一点? 提前致谢
经过多次搜索和测试代码,我解决了这个问题。
我在文件名中添加时间戳如果重复。
在main.js文件中
function checkRepetitive(file) {
var currentList = $scope.fileNavigator.fileList;
var repetitive = false;
angular.forEach(currentList, function (item, index) {
if (item.model.name === file.name) {
repetitive = true
}
});
return repetitive;
}
// click on upload file button in upload modal
$scope.uploadFiles = function () {
angular.forEach($scope.uploadFileList, function (item, index) {
if (checkRepetitive(item)) {
// `morteza.jpg` => `morteza` + `new Date().getTime()).toString()` + `.jpg`
var lastDotIndex = item.name.lastIndexOf('.');
var fileName = item.name.substring(0, lastDotIndex);
var extension = item.name.substring(lastDotIndex, item.name.length);
// name was `read only` so, I can't change that if didn't write this ↓
Object.defineProperty(item, 'name', {
writable: true
});
angular.extend(item, {'name': fileName + (new Date().getTime()).toString() + extension});
} // if (checkRepetitive(item)) {
});