我已经使用html文件输入来选择要上传的文件。我创建了一个角度指令。现在我想为输入设置最大允许的文件大小。如果文件大小大于允许大小,则应由指令返回错误。我是angularjs的新成员,对指令知之甚少。如果有人知道与解释的解决方案,将不胜感激。在angularjs中设置文件输入的最大大小
这是我的代码,直到现在。
HTML
<input type="file" id="flBook" name="flBook" valid-file required accept=".pdf" ng-model="ebook.file" ng-if="ebook.ebook_file.st_filename == undefined">
<span ng-if="formBook.$submitted || formBook.flBook.$touched" ng-cloak>
<span class="text-red" ng-show="formBook.flBook.$valid == false" ng-cloak>E-Book is required.</span>
</span>
JS
app.directive('validFile', function() {
return {
require: 'ngModel',
link: function (scope, el, attrs, ngModel) {
//change event is fired when file is selected
el.bind('change', function() {
scope.$apply(function() {
ngModel.$setViewValue(el.val());
ngModel.$render();
});
});
}
}
});
检查这个问题的文件API的例子,你可以融入你的指令 - http://stackoverflow.com/questions/3717793/javascript-file-upload-size-validation – shershen
@shershen,正如我所说,我是angularjs中的新成员。所以我不知道如何整合到angularjs指令中。那么你可以设置示例吗? –
如果你是新来的角去为@ user3121072告诉你的选项 - 尝试使用现成的指令 - 例如这个高度可配置的插件 - https://github.com/danialfarid/ng-file-upload – shershen