简体   繁体   English

Angular ng-file-upload delete

[英]Angular ng-file-upload delete

Trying to figure best approach to be able to delete a file when using ng-file-upload. 尝试在使用ng-file-upload时找出能够删除文件的最佳方法。 I got my list of files with a list. 我得到了带有列表的文件列表。 My html is: 我的HTML是:

<div>
    <p>Files:</p>
</div>
<div>
    <ul>
        <li ng-repeat="f in files" style="font:smaller">{{f.name}} {{f.$error}} {{f.$errorParam}}</li>
    </ul>
</div>

In my controller, I have: 在我的控制器中,我有:

$scope.$watch('files', function () {
    $scope.upload($scope.files);
});
$scope.$watch('file', function () {
    if ($scope.file != null) {
        $scope.files = [$scope.file];
    }
});
$scope.log = '';


$scope.upload = function (files) {
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
          var file = files[i];

          if (!file.$error) {

            Upload.upload({
                url: (serviceBase + 'api/ppt/docs/upload/multi?transId=' + [$scope.transId]),
                data: {
                  username: $scope.username,
                  file: file,
                  filename: file.name
                }
            }).progress(function (evt) {
                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                $scope.log = 'progress: ' + progressPercentage + '% ' +
                            evt.config.data.file.name + '\n' + $scope.log;
            }).success(function (data, status, headers, config) {
                $timeout(function() {
                    $scope.log = 'file: ' + config.data.file.name + ', Response: ' + JSON.stringify(data) + '\n' + $scope.log;
                });
            }).error(function(data, status, headers, config) {

                var errors = [];
                $scope.errors = data;

                for (var i=0; i<$scope.errors.length; i++)
                {
                    errors.push($scope.errors[i].errMsg);
                }

                $scope.message = "Error uploading files due to: " + errors.join(' ');
            });
          }
        }
    }
};

I see some examples in the documentation, but not seeing best ways to implement, especially if I want individual deletes/remove for each file uploaded rather than just all. 我在文档中看到了一些示例,但没有看到实现的最佳方法,特别是如果我希望对每个上传的文件进行单独的删除/删除,而不仅仅是所有文件。

Thanks much. 非常感谢。

I suppose the straightforward approach will do here. 我想这个直截了当的做法会在这里做。

  1. Add a deleteFile handler to your scope taking array index of the file being deleted. deleteFile处理程序添加到作用域,获取要删除的文件的数组索引。
  2. Add a link/button/handle for each file to delete it upon clicking and set it's attribute ng-click="deleteFile($index) . $index is created by ng-repeat . 为每个文件添加一个链接/按钮/句柄,以便在单击时删除它,并设置它的属性ng-click="deleteFile($index)$indexng-repeat创建。
  3. In your deleteFile handler call the backend to delete the file and upon success splice the files array to remove the deleted entry. 在deleteFile处理程序中,调用后端删除文件,并在成功时拼接files数组以删除已删除的条目。

HTML: HTML:

<li ng-repeat="f in files">
     {{f.name}} {{f.$error}} {{f.$errorParam}} <a class="deleteHandle" ng-click="deleteFile($index)">&times;</a>
</li>

JS: JS:

$scope.deleteFile = function(idx) {
     var file = $scope.files[idx];
     if (file.isUploaded) {
          $http.delete('/api/files/'+file.id).then(function(response){
               if (response.status == 200) {
                    $scope.files.splice(idx, 1);
               }
         });
      } else {
           $scope.files.splice(idx, 1);
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM