【问题标题】:ng-file-upload is accepting any file type when ngf-accept="'image/*'"当 ngf-accept="'image/*'" 时,ng-file-upload 接受任何文件类型
【发布时间】:2017-05-05 04:25:29
【问题描述】:
一直在使用此按钮上传文本文件,对文件类型的检查对我不起作用,该文件被认为是有效的。
<button ng-hide="uploading" class="btn centered" type="file"
ngf-select="uploadFiles($file, $invalidFiles)" accept="'image/*'"
ngf-max-size="4MB" ngf-accept="'image/*'">
{{(boardingData.profile_pic_url) ? "Change Photo" : "Upload a Photo"}}
</button>
【问题讨论】:
标签:
angularjs
ng-file-upload
【解决方案1】:
我们在应用程序中大量使用ng-file-upload,但我们不使用ngf-accept 指令。但是,您不需要使用它来过滤传入的文件类型。假设您有以下<div> 用于拖动:
<div ngf-drop="" ng-model="files" class="some_class_here" ngf-allow-dir="false">
然后在你的控制器中会有一个作用域变量$scope.files。您只需检查$scope.files 的type 属性即可查看文件类型。如果你想检查以image/ 开头的文件,那么你可以使用这个:
if ($scope.files.startsWith("image/")) {
console.log("You dragged an image file");
// or whatever your logic is
}
您可以使用此信息从控制器适当地处理文件类型。请注意,并非所有文件都显示为具有类型,这一点也需要牢记。
【解决方案2】:
我知道这个问题已经过时了,但我自己仍然使用这个工具,并试图弄清楚如果我没有错误地指定 ngf-accept,为什么我不能省略 accept。原来ngf-accept 需要刻度,而accept 不需要。
仅在 ngf 属性中使用 ' 标记,例如ngf-accept="'image/*'" 或 ngf-pattern,例如ngf-pattern="'image/*'" 或 ngf-pattern="'.jpg,.png'"。
在accept,你应该使用accept="image/*"。