如何获取输入类型=文件的加载进度

How to get onloading progress of input type=file?

本文关键字:文件 加载 类型 输入 何获取 获取      更新时间:2023-09-26

这个问题不是关于在文件上传到服务器时跟踪文件的进度。

在我的项目中,我有~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