gistfile1.txt //在模块下面添加服务 .directive('fileModel', ['$parse', 'fileReader', function ($parse, fileReader) { return { restrict: 'EAC', link: function (scope, element, attrs, ngModel) { var model = $parse(attrs.fileModel); va
//在模块下面添加服务 .directive('fileModel', ['$parse', 'fileReader', function ($parse, fileReader) { return { restrict: 'EAC', link: function (scope, element, attrs, ngModel) { var model = $parse(attrs.fileModel); var modelSetter = model.assign; var imgviewID = attrs["imgViewId"]; var imgView = angular.element(document.querySelector("." + imgviewID)); element.bind('change', function (event) { console.info(imgviewID); $('.update-store-form .upload-msg').html(""); //获取图片大小 var boole = (this.files[0].size / 1000) > 1024; if (boole) { var target = $('.update-store-form .upload-msg').attr("style", "visibility:visible"); showTip("图片大于1M"); return false; } //获取图片的类型 var file = $("#upload-file").val(); var sub = file.substr(file.indexOf(".")); if (!/.(gif|jpg|jpeg|png|gif|jpg|png)$/.test(sub)) { var target = $('.update-store-form .upload-msg').attr("style", "visibility:visible"); showTip("图片类型不正确"); return false; } scope.$apply(function () { modelSetter(scope, element[0].files[0]); }); //附件预览 scope.file = (event.srcElement || event.target).files[0]; fileReader.readAsDataUrl(scope.file, scope).then(function (result) { imgView.attr("src", result); }); }); } } }]) .factory('fileReader', ["$q", "$log", function ($q, $log) { var onLoad = function (reader, deferred, scope) { return function () { scope.$apply(function () { deferred.resolve(reader.result); }); } } var onError = function (reader, deferred, scope) { return function () { scope.$apply(function () { deferred.reject(reader.result); }); }; }; var getReader = function (deferred, scope) { var reader = new FileReader(); reader.onload = onLoad(reader, deferred, scope); reader.onerror = onError(reader, deferred, scope); return reader; }; var readAsDataURL = function (file, scope) { var deferred = $q.defer(); var reader = getReader(deferred, scope); reader.readAsDataURL(file); return deferred.promise; } return { readAsDataUrl: readAsDataURL }; }]) //////////////////////////////在html下的 图片标签 ///////////////////////////////仅支持JPG图片,且文件小于1M,尺寸
为200*200像素,请确认是否清晰。