ng-file-upload:将自定义文件格式更改为所有文件,它接受要上载的被阻止文件类型

时间:2017-08-31 22:13:35

标签: angularjs html5 ng-file-upload

我有以下代码只接受图像和pdf文件,但是如果我从文件选择窗口更改格式(我认为它是由Chrome本身提供的)自定义文件到所有文件,它接受所有文件格式,包括zip文件夹。

<button type="button" class="btn-submit upload for-hover smooth-hover"
        data-prod-idx="{{index}}" data-prod-id="{{prod.ProductId}}"
        ng-click="vm.onUpload($event, index, prod.ProductId)"
        ngf-select="vm.uploadAttachments($event, $files, $invalidFiles)"
        multiple accept="application/pdf, image/*" 
        ngf-capture="'camera'"
        ngf-max-size="5MB">Upload Documents
</button>

如何停止上传除image和pdf之外的文件?(对于所有浏览器,请不要使用JQuery)

我尝试在接受中使用不同的值,但这对我没有帮助。

1 个答案:

答案 0 :(得分:1)

使用每个file.typeFile object属性来确定它是否是不合适的类型。在uploadAttachments函数中,如果类型不合适,请跳过上传。

The DEMO

&#13;
&#13;
angular.module("app",['ngFileUpload'])
.controller("ctrl", function () {
  vm = this;
  vm.uploadAttachments = (event, fileList) => {
    console.log("fileList.length=", fileList.length);
    var validList = fileList.filter( f => {
      if (f.type.startsWith("application/pdf")) return true;
      if (f.type.startsWith("image/")) return true;
      console.log("skipping", f.name);
      return false;
    })
    console.log("validList.length=",validList.length);
  };
})
&#13;
  <script src="//unpkg.com/angular/angular.js"></script>
  <script src="//unpkg.com/ng-file-upload/dist/ng-file-upload-all.js"></script>
  <body ng-app="app" ng-controller="ctrl as vm">
    <button type="button" class="btn-submit upload for-hover smooth-hover"
        data-prod-idx="{{index}}" data-prod-id="{{prod.ProductId}}"
        ng-click="vm.onUpload($event, index, prod.ProductId)"
        ngf-select="vm.uploadAttachments($event, $files, $invalidFiles)"
        multiple accept="application/pdf, image/*" 
        ngf-capture="'camera'"
        ngf-max-size="5MB">Upload Documents
    </button>
  </body>
&#13;
&#13;
&#13;