angularjs的问题:点击”选择图片“后,需要很长的时间才能选择图片
问题描述
如下图,点击“选择图片”
然后过了约6秒,系统弹出框来,选择图片
其他功能能正常使用。尝试了很多次,每次都要较长时间才能进行图片选择。为什么需要那么长的时间才能选择图片?
以下是我的代码:
HTML
<!DOCTYPE html>
<html ng-app="fileUpload">
<head>
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>AngularJs</title>
<script type="text/javascript" src="js/lib/angular/angular.js"></script>
<script type="text/javascript" src="js/lib/angular/ng-file-upload.min.js"></script>
<script type="text/javascript" src="js/lib/angular/ng-file-upload-shim.min.js"></script>
<script type="text/javascript" src="js/lib/angular/ng-img-crop.js"></script>
</head>
<body ng-app="fileUpload" ng-controller="MyCtrl">
<form name="myForm">
<div>Crop Image and Upload</div>
<button ngf-select ng-model="picFile" accept="image/*">Select Picture</button>
<div ngf-drop ng-model="picFile" ngf-pattern="image/*" class="cropArea">
<img-crop image="picFile | ngfDataUrl" result-image="croppedDataUrl" ng-init="croppedDataUrl=''"></img-crop>
</div>
<div><img ng-src="{{croppedDataUrl}}" /></div>
<button ng-click="upload(croppedDataUrl, picFile.name)">Submit</button>
<span class="progress" ng-show="progress >= 0">
<div style="width:{{progress}}%" ng-bind="progress + '%'"></div>
</span>
<span ng-show="result">Upload Successful</span>
<span class="err" ng-show="errorMsg">{{errorMsg}}</span>
</form>
</body>
</html>
javascript
<script type="text/javascript">
var app = angular.module('fileUpload', ['ngFileUpload', 'ngImgCrop']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
$scope.upload = function (dataUrl, name) {
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {
file: Upload.dataUrltoBlob(dataUrl, name)
},
}).then(function (response) {
$timeout(function () {
$scope.result = response.data;
});
}, function (response) {
if (response.status > 0) $scope.errorMsg = response.status
+ ': ' + response.data;
}, function (evt) {
$scope.progress = parseInt(100.0 * evt.loaded / evt.total);
});
}
}]);
</script>
CSS
<style type="text/css">
.cropArea {background: #E4E4E4; overflow: hidden; width:500px; height:350px; }
form .progress {line-height: 15px; }
.progress {display: inline-block; width: 100px; border: 3px groove #CCC; }
.progress div {font-size: smaller; background: orange; width: 0; }
img-crop {width: 100%; height: 100%; display: block; position: relative; overflow: hidden; }
img-crop canvas {display: block; position: absolute; top: 50%; left: 50%; outline: none; -webkit-tap-highlight-color: rgba(255, 255, 255, 0);}
</style>
【参考】
上面的代码来自:http://jsfiddle.net/danialfar...
ng-file-upload介绍:https://github.com/danialfari...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有可能是因为没有设置上传文件类型的原因,我之前也是有这样的问题改成
accept: 'image/jpg,image/jpeg,image/png'
就好了accept
会在计算机中进行过滤,所有打开很慢。换成ngf-pattern
过滤,如何不符合ngf-pattern="image/*
的话,返回值是null
,不会是你上传的文件。