2016-06-10 12 views
1

Ich habe ein Angular-Projekt, das einen sehr schönen Timeout Toaster hat, der erscheint, wenn Anfragen zu langsam sind. Aber das Problem ist, ich brauche viel längere Timeouts oder Timeout-Reset während eines Datei-Uploads (ich benutze ng-Datei-Upload auf s3-Speicher).

Meine Frage ist: Wie kann ich bei jedem Fortschritt Antworten ein $ http Timeout zurückgesetzt oder es zu einem gewissen massiven Zahl nur ändern, während Datei-Uploads:

Hier ist mein Interceptor Code in meine Config-Funktion:

$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); 
      } 
     } 
    }) 

Hier ist meine Upload-Funktion:

$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"; 
             }); 
            }); 
           }; 

Antwort

1

$http ‚s timeout option Versprechen akzeptiert:

Timeout - {Anzahl | Versprechen} - Timeout in Millisekunden, oder Versprechen, dass die Anfrage abgebrochen werden sollte, wenn gelöst. Diese

bedeutet, dass es ein Versprechen sein, die Umfragen globale Variable

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

oder implementiert eine andere Logik, die den Fall passt.

+0

Danke aufgelöst eine globale Var für Timeout und scheint zu arbeiten – johan

Verwandte Themen