我有文件上传条件所以onSelect我已经实现了条件,fileNames
变量获取当前用户选择但尚未保存到数据库的文件列表。 AssessmentData
是保存在数据库中的Json响应。那么,如果fileNames
中已存在assessmentData
,则如何设置条件会抛出错误并阻止用户添加相同的fileName ..
main.js
rcsaAssessmentService.getAlldocs(id).then(function(response){
assessmentData = response.data;
});
$scope.onSelect = function (e) {
var fileNames = $.map(e.files, function(file) { return file.name; }).join(', ');
selectedFile.forEach(function (file) {
if (fileNames === file) {
e.preventDefault();
$timeout(function () {
$scope.showErr= true;
});
}
else {
$scope.showErr = false;
}
});
for ( var i=0; i < e.files.length; i++){
selectedFile.push(e.files[i].name);
}
};
JSON.JS
评估数据回复
[
{
"attchDesc": "test ",
"attchTypLookUpCD": null,
"riskAssessmentKey": null,
"uploadedTs": "2015-07-06",
"fileName": "Risk Assessment_SIT Open Defects_0409.xlsx",
"uploadedBy": "ZKZ1KCY",
"riskAssessmentAtachKey": 1115,
"file": null
},
{
"attchDesc": "test",
"attchTypLookUpCD": null,
"riskAssessmentKey": null,
"uploadedTs": "2015-07-06",
"fileName": "abc.xlsx",
"uploadedBy": "ZKZ1KCY",
"riskAssessmentAtachKey": 1116,
"file": null
}
]
答案 0 :(得分:1)
我不确定selectedFile是什么,但这是一个查看值是否在数组中的解决方案:
rcsaAssessmentService.getAlldocs(id).then(function(response){
assessmentData = response.data;
});
$scope.onSelect = function (e) {
var fileNames = assessmentData.map(function(assesment){
return assessment.fileName;
});
e.files.forEach(function (file) {
if (fileNames.indexOf(file.name) !== -1) { // filename was present
e.preventDefault();
$timeout(function () {
$scope.showErr= true;
});
}
else {
$scope.showErr = false;
}
});
for ( var i=0; i < e.files.length; i++){
selectedFile.push(e.files[i].name);
}
};