2015-06-28 92 views
5

我在angularjs上使用$ http,並且我有一個相當大的請求發送。

我想知道是否有辦法做這樣的事情:

content = "I'm a very long content string!" 
$http.post content, url, 'gzip' 

,並有發佈請求的內容自動gzip壓縮,並添加適當的請求頭,這樣服務器就知道解壓內容並將其正確傳遞到控制器

我可以gzip我的內容,並在服務器上手動重新打開它,但我認爲應該有一些方法來自動執行它。在那兒?

+0

將這項工作,爲您的目的是什麼? http://onehungrymind.com/zip-parsing-jszip-angular/ 這是jszip:https://stuk.github.io/jszip/ – Boris

+0

這是一個很酷的軟件包,但它是關於在客戶端打開一個zip文件,我希望我的文本被壓縮到發送請求到服務器 – Yossale

+0

好吧,這是有道理的;也許js-deflate可能會工作:https://github.com/dankogai/js-deflate 看來其他人也對此感興趣,但客戶端 - 服務器通信並不容易:https://stackoverflow.com/questions/424917/why-cant-browser-send-gzip-request – Boris

回答

0

this後,這樣你可以給模型上的參數,以便服務器可以決定如果內容是一個文件,如果文件應解壓的第一

function Ctrl($scope, $http) { 

    //a simple model to bind to and send to the server 
    $scope.model = { 
     gzip: true, 
     file: true 
    }; 

    //an array of files selected 
    $scope.files = []; 

    //listen for the file selected event 
    $scope.$on("fileSelected", function (event, args) { 
     $scope.$apply(function() {    
      //add the file object to the scope's files collection 
      $scope.files.push(args.file); 
     }); 
    }); 

    //the save method 
    $scope.save = function() { 
     $http({ 
      method: 'POST', 
      url: "/Api/PostStuff", 
      //IMPORTANT!!! You might think this should be set to 'multipart/form-data' 
      // but this is not true because when we are sending up files the request 
      // needs to include a 'boundary' parameter which identifies the boundary 
      // name between parts in this multi-part request and setting the Content-type 
      // manually will not set this boundary parameter. For whatever reason, 
      // setting the Content-type to 'false' will force the request to automatically 
      // populate the headers properly including the boundary parameter. 
      headers: { 'Content-Type': false }, 
      //This method will allow us to change how the data is sent up to the server 
      // for which we'll need to encapsulate the model data in 'FormData' 
      transformRequest: function (data) { 
       var formData = new FormData(); 
       //need to convert our json object to a string version of json otherwise 
       // the browser will do a 'toString()' on the object which will result 
       // in the value '[Object object]' on the server. 
       formData.append("model", angular.toJson(data.model)); 
       //now add all of the assigned files 
       for (var i = 0; i < data.files; i++) { 
        //add each file to the form data and iteratively name them 
        formData.append("file" + i, data.files[i]); 
       } 
       return formData; 
      }, 
      //Create an object that contains the model and files which will be transformed 
      // in the above transformRequest method 
      data: { model: $scope.model, files: $scope.files } 
     }). 
     success(function (data, status, headers, config) { 
      alert("success!"); 
     }). 
     error(function (data, status, headers, config) { 
      alert("failed!"); 
     }); 
    }; 
};