Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用$http从angularjs发送压缩post请求_Angularjs_Http - Fatal编程技术网

使用$http从angularjs发送压缩post请求

使用$http从angularjs发送压缩post请求,angularjs,http,Angularjs,Http,我在angularjs上使用$http,我有一个相当大的请求要发送 我想知道是否有这样的方法: content = "I'm a very long content string!" $http.post content, url, 'gzip' 并自动压缩请求后内容,并添加适当的请求头,以便服务器知道如何解压缩内容并将其正确传递给控制器 我可以在我这边gzip内容,并在服务器上手动重新打开它,但我认为应该有一些方法可以自动完成。有吗?请参阅文章,您可以在模型上提供一个参数,以便服务器可以决定

我在angularjs上使用$http,我有一个相当大的请求要发送

我想知道是否有这样的方法:

content = "I'm a very long content string!"
$http.post content, url, 'gzip'
并自动压缩请求后内容,并添加适当的请求头,以便服务器知道如何解压缩内容并将其正确传递给控制器

我可以在我这边gzip内容,并在服务器上手动重新打开它,但我认为应该有一些方法可以自动完成。有吗?

请参阅文章,您可以在模型上提供一个参数,以便服务器可以决定内容是否为文件,以及是否应首先解压缩该文件

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!");
        });
    };
};
函数Ctrl($scope,$http){
//绑定并发送到服务器的简单模型
$scope.model={
gzip:是的,
档案:真
};
//选定的文件数组
$scope.files=[];
//侦听所选文件事件
$scope.$on(“fileSelected”,函数(事件,参数){
$scope.$apply(函数(){
//将文件对象添加到作用域的文件集合
$scope.files.push(args.file);
});
});
//保存方法
$scope.save=函数(){
$http({
方法:“POST”,
url:“/Api/PostStuff”,
//重要提示!!!您可能认为应该将其设置为“多部分/表单数据”
//但事实并非如此,因为当我们发送文件时,请求
//需要包括标识边界的“边界”参数
//此多部件请求中部件之间的名称和设置内容类型
//手动将不会设置此边界参数。无论出于何种原因,
//将内容类型设置为“false”将强制自动删除请求
//正确填充标题,包括边界参数。
标题:{“内容类型”:false},
//此方法将允许我们更改将数据发送到服务器的方式
//为此,我们需要将模型数据封装在“FormData”中
转换请求:功能(数据){
var formData=new formData();
//需要将json对象转换为json的字符串版本,否则
//浏览器将对对象执行“toString()”,这将导致
//在服务器上的值“[Object]”中。
append(“model”,angular.toJson(data.model));
//现在添加所有分配的文件
对于(var i=0;i
这对您有用吗?这里是jszip:这是一个很酷的包,但是它是关于在客户端打开一个zip的,我希望我的文本在发送到服务器的post请求中被压缩,这是有意义的;也许js deflate可能会起作用:似乎其他人也对此感兴趣,但客户机-服务器通信并不容易:在我处于类似情况下,您是否解决过此问题?请查看以下答案,其中详细介绍了此主题: