如何使用当前选定的文件检查数组中的属性?

时间:2015-07-06 19:40:24

标签: javascript angularjs

我有文件上传条件所以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
    }
]

1 个答案:

答案 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);
  }
};