javascript - angular $http $watch 文件上传过程中的进度

标签 javascript angularjs

我有这样一个函数:

$scope.fileUpload = function (files) {
                angular.forEach(files, function () {
                    var fd = new FormData();
                    fd.append('files[]', files[0]);

                    $http.post('/my-url', fd, {
                        transformRequest: angular.identity,
                        headers: {
                            'Content-Type': undefined}
                    }).then(function (data) {
                        console.log(data);
                    });
                });
            };

和模板:

<input type="file"
               name="files[]"
               id="fileUploaderButton_contentFormFiles"
               onchange="angular.element(this).scope().fileUpload(files)"
               multiple>
<div class="progress" id="fileUploaderProgressbar_contentFormFiles">
    <div class="progress-bar progress-bar-striped active"
         role="progressbar"
         aria-valuenow="{{progress}}"
         aria-valuemin="0"
         aria-valuemax="100"
         style="min-width: 2em; width: {{progress}}%">
        <span>{{progress}}%</span>
    </div>
</div>

我如何 $watch 上传文件的传输值(字节),以便我可以在 $http 请求期间更新进度条中的 {{progress}} (使用 native Angular 特征)?提前致谢。

最佳答案

不是你想要的答案但是你的做法是错误的

你应该使用 ng-file-upload这是对此的标准 Angular 方法,并支持上传进度跟踪

关于javascript - angular $http $watch 文件上传过程中的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35529567/

相关文章:

javascript - 使用 PHP 和 MySQL 的多重下拉消除形式

javascript - 使用 Yup 验证多个输入字段

javascript - 如何在单击按钮时在一行 ng-repeat 中使输入只读

javascript - 如何将 select2 4.0 与 angularjs 一起使用,何时初始化它

javascript - 当新旧值相同时,Angular watch 会被解雇

javascript - Vue SFC/vue-loader/webpack 样式包含顺序

javascript - 不等间隔 Highcharts 仪表

javascript - AngularJS $HTTP.json

javascript - AngularJs - 将 2 个关键相似的数组合并为 1 个 ng-repeat

javascript - ng-change 事件在 ng-repeat 中仅触发一次