45

按下提交按钮后,一组文件($scope.files,可以小到一个文件,用户想要多少就多少)通过我FormDataXMLHttpRequest角度函数提交$scope.uploadFiles

$scope.uploadFiles = function() {
    for (var i in $scope.files) {
        var form = new FormData();
        var xhr = new XMLHttpRequest;

            // Additional POST variables required by the API script
        form.append('destination', 'workspace://SpacesStore/' + $scope.currentFolderUUID);
        form.append('contenttype', 'idocs:document');
        form.append('filename', $scope.files[i].name);
        form.append('filedata', $scope.files[i]);
        form.append('overwrite', false);

        xhr.upload.onprogress = function(e) {
            // Event listener for when the file is uploading
            $scope.$apply(function() {
                var percentCompleted;
                if (e.lengthComputable) {
                    percentCompleted = Math.round(e.loaded / e.total * 100);
                    if (percentCompleted < 1) {
                                            // .uploadStatus will get rendered for the user via the template
                        $scope.files[i].uploadStatus = 'Uploading...';
                    } else if (percentCompleted == 100) {
                        $scope.files[i].uploadStatus = 'Saving...';
                    } else {
                        $scope.files[i].uploadStatus = percentCompleted + '%';
                    }
                }
            });
        };

        xhr.upload.onload = function(e) {
            // Event listener for when the file completed uploading
            $scope.$apply(function() {
                $scope.files[i].uploadStatus = 'Uploaded!'
                setTimeout(function() {
                    $scope.$apply(function() {
                        $scope.files[i].uploadStatus = '';
                    });
                }, 4000);
            });
        };

        xhr.open('POST', '/path/to/upload/script');
        xhr.send(form);
    }

}

问题是var i每个文件的初始 for 循环中的增量,并且到事件侦听器触发时,i已经增量超过files[i]所需的预期值,仅影响数组中的最后一个。我使用.uploadStatus这种方式以交互方式向用户显示每个单独文件的进度,因此我需要为$scope.files. 如何为 Angular 中的单个数组元素分配和跟踪事件?

更新


我重新设计了两个事件监听器,取得了一些成功,但我仍然遇到奇怪的行为:

xhr.upload.onprogress = (function(file) {
    // Event listener for while the file is uploading
    return function(e) {
        $scope.$apply(function() {
            var percentCompleted = Math.round(e.loaded / e.total * 100);
            if (percentCompleted < 1) {
                file.uploadStatus = 'Uploading...';
            } else if (percentCompleted == 100) {
                file.uploadStatus = 'Saving...';
            } else {
                file.uploadStatus = percentCompleted + '%';
            }
        });
    }
})($scope.files[i]);

xhr.upload.onload = (function(file, index) {
    // Event listener for when the file completed uploading
    return function(e) {
        $scope.$apply(function() {
            file.uploadStatus = 'Uploaded!'
            setTimeout(function() {
                $scope.$apply(function() {
                    $scope.files.splice(index,1);
                });
            }, 2000);
        });
    }
})($scope.files[i], i);

.onprogress似乎顺利进行,但是对 进行了一些小的更改.onload,我现在看到 AngularJS 对其模板的双向绑定有很多奇怪的行为。对于数组中的每个元素$scope.files,使用上述.uploadStatus属性给出一个状态。现在我通过传入自执行函数setTimeout的变量拼接数组中的元素。i奇怪的是,现在一次上传的上限约为 6 个同时上传,这一定是服务器端的问题,但我注意到随着数组元素的拼接,ng-repeat模板中元素,不一定是它应该的元素。我还注意到,在达到 2000 毫秒阈值后,经常有条目没有被拼接。

这让人想起原始问题,即i在整个事件侦听器的触发过程中引用变量时不可靠?现在我将它传递给匿名自执行.onload函数,并且拼接使用它来确定它应该删除哪个数组元素,但它不一定会删除正确的元素,并且经常在数组中留下其他元素应该删除它们。

4

2 回答 2

6

您传递给事件处理程序的index是指原始数组中的索引。每次成功调用 后splice,数组更改和索引不再指向同一事物。

$scope.files = ['a.jpg', 'b.jpg', 'c.jpg'];
// $scope.files[1] = 'b.jpg'

$scope.files.splice(1,1);
// $scope.files = ['a.jpg', 'c.jpg']
// $scope.files[1] = 'c.jpg'
于 2012-11-28T12:11:44.620 回答
4

AngularJs 1.5.5 通过为上传进度提供回调事件处理程序来改变方法。

参考 -如何使用 $http 或 $resource 在 angularjs 1.5.5 中使用 eventHandlers 和 uploadEventHandlers 跟踪进度?

var uploadData = new FormData();
uploadData.append('file', obj.lfFile);
var fileData = angular.toJson({
    'FileName': obj.lfFile.name,
    'FileSize': obj.lfFile.size
});
uploadData.append('fileData', fileData)

$http({
    method: 'POST',
    url: vm.uploadPath,
    headers: {
        'Content-Type': undefined,
        'UserID': vm.folder.UserID,
        'ComputerID': vm.folder.ComputerID,
        'KeepCopyInCloud': vm.keepCopyInCloud,
        'OverWriteExistingFile': vm.overwriteExistingFile,
        'RootFileID': vm.folder.RootFileID,
        'FileName': obj.lfFile.name,
        'FileSize': obj.lfFile.size
    },
    eventHandlers: {
        progress: function(c) {
            console.log('Progress -> ' + c);
            console.log(c);
        }
    },
    uploadEventHandlers: {
        progress: function(e) {
            console.log('UploadProgress -> ' + e);
            console.log(e);
        }
    },
    data: uploadData,
    transformRequest: angular.identity
}).success(function(data) {
    console.log(data);
}).error(function(data, status) {
    console.log(data);
    console.log(status);
});
于 2016-07-06T15:49:44.623 回答