【问题标题】:Cancel Angular File Upload取消 Angular 文件上传
【发布时间】:2016-04-27 09:56:57
【问题描述】:

我无法取消来自 Angular 的上传文件请求,我正在使用下面提到的插件上传文件,根据作者的说法,这感觉很简单,但我不确定我应该如何实现它。这就是我上传文件的方式,从Angular插件文件发布到php,php实习生读取整个文件并通过curl将其流式传输到java api,所以,现在我有一个取消按钮,一旦用户点击取消上传按钮我需要能够停止从角度插件上传,所以一旦上传停止,我可以再次调用清理半流文件....和其他清理内容。

插件链接:https://github.com/danialfarid/ng-file-upload

$scope.uploadFiles = function(files) {

if(files != undefined){
    $scope.currentFileUploadList = files;
    $scope.$apply();
}
angular.forEach(files, function(file, key) {
    if(isSpecialChar(file.name)){
        //$scope.currentFileUploadList[key].status = false;
        //$scope.$apply();
        file.upload = Upload.upload({
            url: $scope.BASE_FOLDER+'/sync/file/upload',
            fields: {csrf_token: $('.csrf_token').html(), path: 'ParaBlu'},
            file: file
        });

        file.upload.then(function (response) {
            $timeout(function () {
                file.result = response.data;
                if(response.data.result == 'success'){
                    $('.status').hide();
                    toastr.success(response.data.file.fileName+' has been uploaded successfully', 'Successfully!!!', {allowHtml: true});
                    $scope.currentFileUploadList.forEach(function(value, key){
                        if(value.name.toString() == response.data.file.fileName.toString()){
                            $scope.currentFileUploadList.splice(key, 1);
                        }
                    });
                    if((files.length) == key){
                        $('#uploadFiles').modal('hide');
                    }
                }else{
                    toastr.error(file.name, response.data.msg, 'Fail!!!', {allowHtml: true});
                }
            });
        }, function (response) {
            if (response.status > 0)
                $scope.errorMsg = response.status + ': ' + response.data;
        });

        file.upload.progress(function (evt) {
            file.progress = Math.min(100, parseInt(100.0 *
                evt.loaded / evt.total));
        });
    }else{
        $scope.currentFileUploadList.forEach(function(value, key){
            if(value.name.toString() == file.name.toString()){
                $scope.currentFileUploadList.splice(key, 1);
            }
        });
        toastr.error('Special characters like ( * \ | : " < > ? / ) are not allowed, in a file name.', 'Fail!!!', {allowHtml: true});
    }
});

};

【问题讨论】:

  • /* cancel/abort the upload in progress. */ upload.abort(); /*
  • 复制自官方文档。一个按钮 - 一个回调,然后通过向后端发送请求来处理您需要为后端做的任何事情。
  • 所以你,,应该这样工作,或者我应该为点击添加一个事件监听器并调用上传.abort() 函数。
  • 试一试。任何工作都可以,不会超过 2-3 分钟。
  • 我都试过了,都没有用。

标签: angularjs file-upload upload ng-file-upload cancel-button


【解决方案1】:

不是插件的问题,而是流媒体的问题,我不得不单独取消php文件流媒体以取消上传,处理,不确定是吗?我虽然在 php.ini 中将用户中断设置为 false 也会停止 php curl 请求,一旦取消 ajax 请求但它没有发生,无论如何感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 2014-05-08
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2023-03-11
    • 2013-09-27
    • 2015-11-21
    相关资源
    最近更新 更多