关于“angularJS与Plupload一起工作”的一些问题

时间:2013-09-10 07:27:40

标签: javascript angularjs web plupload

在Directly中,我想删除上传器中的文件,我这样做了

 $scope.close = function() {
        console.log($scope);
        $scope.isOpen = false;
        $('.file_container').empty();
        **angular.forEach($scope.uploader.files, function(v, k) {
            $scope.uploader.removeFile(v);
            $scope.fileLength = $scope.uploader.files.length;
        });**

    };

    $scope.uploader = new plupload.Uploader({
        runtimes: 'html5,html4',
        browse_button: 'addFile',
        max_file_size: '20000mb',
        chunk_size: '512kb',
        // resize: { width: 125, height: 85, quality: 90 },
        flash_swf_url: '../scripts/lib/plupload.flash.swf',
        filters: [{
                extensions: 'jpg,png'
            }]
    });

    $scope.fileLength = 0;
    $scope.currentUpload = 0;
    $scope.uploader.bind('init', function() {
        console.log('init');
    });
    $scope.uploader.bind('UploadProgress', function(up, files) {
        $('.progress_percent').eq($scope.currentUpload).html(files.percent + '%');
        $('.progress_bar').eq($scope.currentUpload).css('width', files.percent + '%');
    });
    $scope.uploader.bind('FilesAdded', function(up, files) {
        $scope.$apply(function() {
            $scope.fileLength = files.length;
        });
        console.log($('input[type=file]'));
        readURL($('input[type=file]')[0]);
    //    up.start();
    });
    $scope.uploader.bind('BeforeUpload', function(up, file) {
        var mediaName = 'MediaBank' + Math.random().toString().substr(2);
        up.settings.url = '/Upload.ashx?medianame=' + mediaName + '&activityid=2013';
    });
    $scope.uploader.bind('FileUploaded', function(up, file, info) {
        $scope.currentUpload++;
        console.log($scope.currentUpload);
    });
    $scope.uploader.init();
    $('#save').bind('click', function() {
        $scope.uploader.start();
    });

而且,当我添加2个文件时,以及调用“$ scope.close”函数时,只删除了1个文件...为什么?谢谢你们所有人!

1 个答案:

答案 0 :(得分:1)

可能是迭代器的一个问题,以及您在浏览时更改列表的事实。

您可以尝试替换:

**angular.forEach($scope.uploader.files, function(v, k) {
            $scope.uploader.removeFile(v);
            $scope.fileLength = $scope.uploader.files.length;
        });**

with:

$scope.uploader.splice(0); // $scope.uploader.splice(); should work too
$scope.fileLength = $scope.uploader.files.length;

希望这会有所帮助