此问题与在将文件上传到服务器时跟踪文件的进度无关。
在我的项目中,我需要读取~6GB .csv文件,并根据特定条件过滤掉一些行。
使用已知事件,很容易知道文件何时被加载到浏览器中,但由于我正在处理相当大的CSV,我想向用户显示该文件加载到浏览器的百分比
要加载CSV,我使用以下自定义指令:
angular.module('csv-filter')
.directive('fileReader', function() {
return {
scope: {
fileReader:"="
},
link: function(scope, element) {
$(element).on('change', function(changeEvent) {
var files = changeEvent.target.files;
console.log('files', files);
console.log('waiting for onload event..');
if (files.length) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
var filesize = 0;
scope.$apply(function () {
scope.fileReader = {filename: files[0].name, contents: contents};
});
};
r.readAsText(files[0]);
}
});
}
};
});
以下是我正在执行此操作的应用的链接:https://ahref-csv-filter.herokuapp.com/
答案 0 :(得分:1)
有一个progress事件。你试过吗?
答案 1 :(得分:0)
使用HTML5,您可以使用Ajax和jQuery进行文件上传。
HTML
<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="button" value="Upload" />
</form>
<progress></progress>
现在Ajax使用按钮点击提交:
$(':button').click(function(){
var formData = new FormData($('form')[0]);
$.ajax({
url: 'upload',
type: 'POST',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
myXhr.upload.addEventListener('progress',progressFunction, false);
}
return myXhr;
},
beforeSend: beforeSendHandler,
success: completeHandler,
error: errorHandler,
data: formData,
cache: false,
contentType: false,
processData: false
});
});
现在,如果你想处理进展。
function progressFunction(e){
if(e.lengthComputable){
$('progress').attr({value:e.loaded,max:e.total});
}
}
答案 2 :(得分:0)
您可以使用ng-file-upload
。这是一个非常简洁的指令,使您可以执行多文件上传并跟踪上传事件的进度。
官方Samples
<强>示例强>
//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
$scope.uploadFiles = function(files, errFiles) {
$scope.files = files;
$scope.errFiles = errFiles;
angular.forEach(files, function(file) {
file.upload = Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {file: file}
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt) {
file.progress = Math.min(100, parseInt(100.0 *
evt.loaded / evt.total));
});
});
}
}]);
**strong text**
.thumb {
width: 24px;
height: 24px;
float: none;
position: relative;
top: 7px;
}
form .progress {
line-height: 15px;
}
}
.progress {
display: inline-block;
width: 100px;
border: 3px groove #CCC;
}
.progress div {
font-size: smaller;
background: orange;
width: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
<script src="https://angular-file-upload.appspot.com/js/ng-file-upload-shim.js"></script>
<script src="https://angular-file-upload.appspot.com/js/ng-file-upload.js"></script>
<body ng-app="fileUpload" ng-controller="MyCtrl">
<h4>Upload on file select</h4>
<button ngf-select="uploadFiles($files, $invalidFiles)" multiple
accept="image/*" ngf-max-height="1000" ngf-max-size="1MB">
Select Files</button>
<br><br>
Files:
<ul>
<li ng-repeat="f in files" style="font:smaller">{{f.name}} {{f.$errorParam}}
<span class="progress" ng-show="f.progress >= 0">
<div style="width:{{f.progress}}%"
ng-bind="f.progress + '%'"></div>
</span>
</li>
<li ng-repeat="f in errFiles" style="font:smaller">{{f.name}} {{f.$error}} {{f.$errorParam}}
</li>
</ul>
{{errorMsg}}
</body>
答案 3 :(得分:0)
使用ng-file-upload指令。