2015-06-28 3 views
5

Я использую $ http на angularjs, и у меня есть довольно большая просьба отправить.

Я интересно, если есть способ сделать что-то вроде этого:

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

и имеет содержание после запроса автоматически с gzip'нутым и добавить соответствующий заголовок запроса, поэтому сервер будет знать, чтобы распаковать содержимое и правильно передать его контроллеру

Я могу скрыть содержимое на моей стороне и повторно открыть его вручную на сервере, но я подумал, что должен быть какой-то способ сделать это автоматически. Здесь?

+0

Будет ли это работать для ваших целей? http://onehungrymind.com/zip-parsing-jszip-angular/ Вот jszip: https://stuk.github.io/jszip/ – Boris

+0

Это классный пакет, но речь идет об открытии почтового индекса на клиенте, и я хочу, чтобы мой текст был застегнут на почтовом запросе на сервере – Yossale

+0

Хорошо, что имеет смысл; возможно, js-deflate может работать: https://github.com/dankogai/js-deflate Похоже, другие заинтересованы в этом, но общение между клиентом и сервером не упрощает: https://stackoverflow.com/questions/424917/why-cant-browser-send-gzip-request – Boris

ответ

0

См this пост, как и что вы могли бы дать параметр на модели, так что сервер может решить, если содержание файла, и если файл должен быть unziped первым

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!"); 
     }); 
    }; 
}; 
Смежные вопросы