如何获取输入类型=文件的加载进度
How to get onloading progress of input type=file?
这个问题不是关于在文件上传到服务器时跟踪文件的进度。
在我的项目中,我有~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/
有一个进度事件。你试过吗?
使用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});
}
}
您可以使用ng-file-upload
。这是一个非常简洁的指令,使您能够进行多文件上传并跟踪上传事件的进度。
ng-file-upload
官方样品
//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>
使用ng-file-upload指令。
查看链接http://ngmodules.org/modules/ng-file-upload
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- Ajax文件加载和<输入>文件加载
- 使用Bootstrap将JSON文件加载到表中
- 将组合JSON文件加载到谷歌地图
- 将文本文件加载到javascript中
- jQuery Mobile样式从另一个文件加载内容
- 使用JavaScript将压缩文本文件加载到字符串中
- 数组在手动写入时有效,但从文本文件加载时无效
- 如何在打印前等待javascript文件加载twitter,并在打印后关闭选项卡
- 从文本文件加载jsondata时出现空白页
- AngularJS:如何使用ng-include将javascript文件加载到部分中
- 使用jquery将html文件加载到html中,用于离线/本地项目
- 从另一个grunt文件加载grunt任务
- 将php文件加载到弹出窗口中,并将php变量发布到该弹出窗口中(使用onclick)
- 如何使用node.js将不同的json文件加载到不同的json对象中
- 将变量从服务器上的外部文件加载到 HTML 文档中
- 节点 - 将 JSON 文件加载到代码中
- 如何使用 JavaScript/jQuery 将 XML 文件加载到 JSP/HTML 页面
- NodeJS和ExpressJS-Javascript文件加载,但CSS文件不加载;t
- D3.js:通过Force Layout从JSON文件加载多个网络