即时设置$ http超时

约翰

我有一个Angular项目,它有一个非常漂亮的Timeout烤面包机,如果请求太慢,它会弹出。但是问题是我在文件上传期间需要更长的超时时间或超时重置(即使用ng-file-upload到s3存储)。

我的问题是:如何在每个进度响应期间重置$ http超时,或者仅在文件上传期间如何将其更改为大量?

这是我在config函数中的拦截器代码:

$httpProvider.interceptors.push(function ($rootScope, $q, toaster) {
        return {
            request: function (config) {
                //config.cache = true;
                config.timeout = 6000;
                return config;
            },
            responseError: function (rejection) {
                //console.log(rejection);
                switch (rejection.status) {
                    case -1 :
                        console.log('connection timed out!');
                        toaster.pop({
                            type: 'error',
                            title: 'Server Timed Out!',
                            body: 'Waiting for request timed out! \n Please check your Internet connection and try again!',
                            timeout: 6000
                        });
                        break;
                    case 404 :
                        console.log('Error 404 - not found!');
                        toaster.pop({
                            type: 'error',
                            title: 'Server Timed Out!',
                            body: 'Error 404! \n Server returned: Not found! Please check your Internet connection and try again!',
                            timeout: 6000
                        });
                        break;
                }
                return $q.reject(rejection);
            }
        }
    })

这是我的上传功能:

$scope.upload = function (file) {

                                    $scope.count += 1;
                                    file.id= $scope.count;
                                    var durl = apiserv + "api.upload-s3.php?path=" + $scope.folder;
                                    var arr = [];
                                    arr.filename = file.name;
                                    arr.status = "";
                                    arr.progress = 0;
                                    arr.class = "list-group-item-warning";
                                    $scope.files[file.id] = arr;
                                    $http({url: durl}).then(function (drs) {
                                        console.log(drs);
                                        drs.data.file = file;
                                        Upload.upload({
                                            url: drs.data.action, //S3 upload url including bucket name
                                            method: 'POST',
                                            data: {
                                                key: drs.data.key,
                                                acl: drs.data.acl,
                                                Policy: drs.data.Policy,
                                                'X-Amz-Algorithm' : drs.data['X-Amz-Algorithm'],
                                                'X-Amz-Credential' : drs.data['X-Amz-Credential'],
                                                'X-Amz-Date' : drs.data['X-Amz-Date'],
                                                'X-Amz-Signature' : drs.data['X-Amz-Signature'],
                                                //'Content-Type': file.type !== '' ? file.type : 'application/octet-stream',
                                                file: file
                                            }
                                        }).then(function (resp) {
                                            console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
                                            $scope.files[resp.config.data.file.id].status = "Success";
                                            $scope.files[resp.config.data.file.id].progress = 100;
                                            $scope.files[resp.config.data.file.id].class = "list-group-item-success";
                                        }, function (resp) {
                                            console.log('Error status: ' + resp.status);
                                            $scope.files[resp.config.data.file.id].status = "Error: "+ resp.status;
                                            $scope.files[resp.config.data.file.id].progress = 0;
                                            $scope.files[resp.config.data.file.id].class = "list-group-item-danger";
                                        }, function (evt) {
                                            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                                            //console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
                                            console.log(evt.config.data.file);
                                            $scope.files[evt.config.data.file.id].status = "Uploading...";
                                            $scope.files[evt.config.data.file.id].progress = progressPercentage;
                                            $scope.files[resp.config.data.file.id].class = "list-group-item-warning";
                                        });
                                    });
                                };
那将是烧瓶

$httptimeout选项接受承诺:

超时– {number | Promise} –超时(以毫秒为单位),或承诺在解决后应中止请求。

这意味着可以轮询全局变量

config.timeout = $q(function (resolve) {
  var i = 0;
  var interval = setInterval(function () {
    i++;
    if (i * 1000 >= $rootScope.httpTimeout) {
      resolve();
      $rootScope.$apply();
      clearInterval(interval);
    });
  }, 1000);
});

或实施其他适合情况的逻辑。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Ruby 1.9.3中设置HTTP超时

来自分类Dev

如何设置Net :: HTTP.start的超时时间?

来自分类Dev

FSharp.Data HTTP请求-无法设置请求超时

来自分类Dev

使用HTTP.jl设置获取请求的超时

来自分类Dev

Java HTTP客户端请求设置超时

来自分类Dev

Apache HTTP组件:在默认客户端上设置超时

来自分类Dev

如何设置Net :: HTTP.start的超时时间?

来自分类Dev

iOS:设置httpBody属性时,HTTP POST请求超时

来自分类Dev

如何为Django Rest HTTP请求设置超时

来自分类Dev

Paypal Sandbox Vault:C#操作已超时(HTTP请求超时设置为30000ms)

来自分类Dev

即时设置车把类

来自分类Dev

即时编辑HTTP请求

来自分类Dev

即时编辑HTTP请求

来自分类Dev

如何使用android-async-http:1.4.7在http发布中设置超时?

来自分类Dev

使用Rails在Net :: HTTP :: Get.new中设置自定义超时

来自分类Dev

如何使用超时设置解决AWS ELB / EC2 HTTP 503?

来自分类Dev

杰克逊JSON如何设置HTTP连接和读取超时

来自分类Dev

自定义HTTP类Angular 2来更改基本URL并设置超时

来自分类Dev

如何为PHP的Amazon AWS开发工具包设置HTTP超时

来自分类Dev

如何设置骆驼HTTP4连接超时选项?

来自分类Dev

为入站网关回复超时设置HTTP状态代码吗?

来自分类Dev

gcloud-node是否可以设置将文件上传到存储的http超时?

来自分类Dev

自定义HTTP类Angular 2来更改基本URL并设置超时

来自分类Dev

在ASP.NET MVC 5中即时更改会话超时

来自分类Dev

设置全局CURL超时

来自分类Dev

在MessageBoxManager上设置超时

来自分类Dev

jQuery无法设置超时

来自分类Dev

设置MBProgressHUD超时

来自分类Dev

设置超时时间