如何在拖放事件后停止 ng-flow 插件的上传操作
How to stop upload action for ng-flow plugin after drag and drop event
我使用简单的拖放效果基于 ng-flow 下载时提供的简单图片上传示例。拖放工作正常,但我想停止默认上传操作,因为我想使用表单提交操作或使用 Angular .post()
函数上传图像:
<html ng-app="app" ng-controller="imageController" flow-init
flow-file-added="!!{png:1,gif:1,jpg:1,jpeg:1}[$file.getExtension()]"
flow-files-submitted="$flow.upload()">
<head>
<title>Image</title>
<!-- <script src="../../bower_components/angular/angular.js"></script> -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="../../js/ng-flow-standalone.js"></script>
<script src="app.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet"/>
<style>
.thumbnail {
max-width: 200px; max-height: 150px; line-height: 20px; margin-bottom: 5px;
}
</style>
</head>
<body flow-prevent-drop>
<div class="container" >
<h1>flow image example</h1>
<hr class="soften"/>
<div>
<div class="thumbnail" ng-hide="$flow.files.length"
flow-drop flow-drag-enter="style={border:'4px solid green'}" flow-drag-leave="style={}" ng-style="style">
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" />
</div>
<div class="thumbnail" ng-show="$flow.files.length">
<img flow-img="$flow.files[0]" />
</div>
<div>
<a href="#" class="btn" ng-hide="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}">Select image</a>
<a href="#" class="btn" ng-show="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}">Change</a>
<a href="#" class="btn btn-danger" ng-show="$flow.files.length"
ng-click="$flow.cancel()">
Remove
</a>
</div>
<p>
Only PNG,GIF,JPG files allowed.
</p>
</div>
</div>
</body>
</html>
我尝试使用 event.preventDefault();在 "fileAdded" 和 "filesSubmitted" 事件处理程序中,但这并没有停止上传开始事件。我想在表单提交过程中执行上传过程。
当我在"fileAdded"事件中使用"return false"时,根本不会插入图像。
但是,如果我在 "filesSubmitted" 事件处理程序中注入错误,脚本将失败,并且不会触发上传。但这不是实现此效果的干净方法。
我也使用了这段代码,但它阻止了将图像添加到页面:
app.controller('imageController',
['$scope',
function ($scope) {
$scope.$on('flow::fileAdded', function (event, $flow, flowFile) {
console.log('$scope.$on', event, $flow);
event.preventDefault();//prevent file from uploading
console.log("event.preventDefault() executed.")
});
}])
有关详细信息,请参阅下面的快照。
感谢您帮助解决此问题。
我想出了停止触发上传操作所需的条件,以便我们可以在 Angular post() 期间提交图像数据,而不是表单提交操作。
要么删除配置 属性 target
来自:
.config(['flowFactoryProvider', function (flowFactoryProvider) {
flowFactoryProvider.defaults = {
target: 'upload.php',
permanentErrors: [404, 500, 501],
maxChunkRetries: 1,
chunkRetryInterval: 5000,
simultaneousUploads: 4,
singleFile: true,
};
或从以下事件之一调用 .pause()
方法:
$scope.$on('flow::fileAdded', function (event, $flow, flowFile) {
console.log('flow::fileAdded - appraiser signature', event, $flow, flowFile);
...
$scope.processFile(flowFile);
flowFile.pause();
...
});
或者在 .config()
函数中形成这个事件:
flowFactoryProvider.on('fileAdded', function(file, event){
console.log('fileAdded', file, event);
file.pause();
event.preventDefault();
console.log('file upload was paused.');
return true;
});
并且,为了检索所选图像的 Base64 字符串,您需要在控制器中定义以下函数:
$scope.processFile = function(flowFile){
console.log('$scope.processFile', flowFile);
var fileReader = new FileReader();
fileReader.onload = function (event) {
console.log('fileReader.onload - event=', event);
var uri = event.target.result;
$scope.imageString = uri;
$scope.imageStringB64 = uri.replace(/^data:image\/(png|jpg);base64,/, '');
};
fileReader.readAsDataURL(flowFile.file);
};
通过上面的操作,您已经在范围变量 imageStringB64
中检索了图像的 Base64 值,然后您可以使用 $http.post()
函数将其发送到服务器,而不是使用传统的表单提交按钮.
希望以上内容对您有用。
塔雷克
我使用简单的拖放效果基于 ng-flow 下载时提供的简单图片上传示例。拖放工作正常,但我想停止默认上传操作,因为我想使用表单提交操作或使用 Angular .post()
函数上传图像:
<html ng-app="app" ng-controller="imageController" flow-init
flow-file-added="!!{png:1,gif:1,jpg:1,jpeg:1}[$file.getExtension()]"
flow-files-submitted="$flow.upload()">
<head>
<title>Image</title>
<!-- <script src="../../bower_components/angular/angular.js"></script> -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="../../js/ng-flow-standalone.js"></script>
<script src="app.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet"/>
<style>
.thumbnail {
max-width: 200px; max-height: 150px; line-height: 20px; margin-bottom: 5px;
}
</style>
</head>
<body flow-prevent-drop>
<div class="container" >
<h1>flow image example</h1>
<hr class="soften"/>
<div>
<div class="thumbnail" ng-hide="$flow.files.length"
flow-drop flow-drag-enter="style={border:'4px solid green'}" flow-drag-leave="style={}" ng-style="style">
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" />
</div>
<div class="thumbnail" ng-show="$flow.files.length">
<img flow-img="$flow.files[0]" />
</div>
<div>
<a href="#" class="btn" ng-hide="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}">Select image</a>
<a href="#" class="btn" ng-show="$flow.files.length" flow-btn flow-attrs="{accept:'image/*'}">Change</a>
<a href="#" class="btn btn-danger" ng-show="$flow.files.length"
ng-click="$flow.cancel()">
Remove
</a>
</div>
<p>
Only PNG,GIF,JPG files allowed.
</p>
</div>
</div>
</body>
</html>
我尝试使用 event.preventDefault();在 "fileAdded" 和 "filesSubmitted" 事件处理程序中,但这并没有停止上传开始事件。我想在表单提交过程中执行上传过程。
当我在"fileAdded"事件中使用"return false"时,根本不会插入图像。
但是,如果我在 "filesSubmitted" 事件处理程序中注入错误,脚本将失败,并且不会触发上传。但这不是实现此效果的干净方法。
我也使用了这段代码,但它阻止了将图像添加到页面:
app.controller('imageController',
['$scope',
function ($scope) {
$scope.$on('flow::fileAdded', function (event, $flow, flowFile) {
console.log('$scope.$on', event, $flow);
event.preventDefault();//prevent file from uploading
console.log("event.preventDefault() executed.")
});
}])
有关详细信息,请参阅下面的快照。
感谢您帮助解决此问题。
我想出了停止触发上传操作所需的条件,以便我们可以在 Angular post() 期间提交图像数据,而不是表单提交操作。
要么删除配置 属性 target
来自:
.config(['flowFactoryProvider', function (flowFactoryProvider) {
flowFactoryProvider.defaults = {
target: 'upload.php',
permanentErrors: [404, 500, 501],
maxChunkRetries: 1,
chunkRetryInterval: 5000,
simultaneousUploads: 4,
singleFile: true,
};
或从以下事件之一调用 .pause()
方法:
$scope.$on('flow::fileAdded', function (event, $flow, flowFile) {
console.log('flow::fileAdded - appraiser signature', event, $flow, flowFile);
...
$scope.processFile(flowFile);
flowFile.pause();
...
});
或者在 .config()
函数中形成这个事件:
flowFactoryProvider.on('fileAdded', function(file, event){
console.log('fileAdded', file, event);
file.pause();
event.preventDefault();
console.log('file upload was paused.');
return true;
});
并且,为了检索所选图像的 Base64 字符串,您需要在控制器中定义以下函数:
$scope.processFile = function(flowFile){
console.log('$scope.processFile', flowFile);
var fileReader = new FileReader();
fileReader.onload = function (event) {
console.log('fileReader.onload - event=', event);
var uri = event.target.result;
$scope.imageString = uri;
$scope.imageStringB64 = uri.replace(/^data:image\/(png|jpg);base64,/, '');
};
fileReader.readAsDataURL(flowFile.file);
};
通过上面的操作,您已经在范围变量 imageStringB64
中检索了图像的 Base64 值,然后您可以使用 $http.post()
函数将其发送到服务器,而不是使用传统的表单提交按钮.
希望以上内容对您有用。
塔雷克