413使用Angular JS FormData请求实体太大

时间:2017-09-18 03:12:43

标签: javascript php angularjs apache xmlhttprequest

我在使用413 Request Entity Too Large在请求正文中发送序列化图片以及其他POST数据时收到FormData错误。

我在服务器上使用PHP 5.x / Apache在前端运行angular 1.6.0。

我这样在正文中发送文件的原因是因为我想为用户提供编辑某些配置文件数据和/或同时更新其配置文件图像的选项。

我正在准备并发送请求如下:

$scope.sendRequest = function(){

    var data = {},
        header = {},
        file = $scope.file; // this is the base64 version of the image uploaded

    if(!empty(file)){ // user has uploaded an image

        data = new FormData();
        data.append('file', file);

        var user = $scope.user; // this is all other non-image form data
        for(var i in user){
            if(!empty(user[i])) data.append(i, $scope.user[i]);
        }

        header = { 
            transformRequest: angular.identity, 
            headers: {  
                "Content-Type" : undefined, // the is the only setting that works but I admit, its odd to set this to "undefined" - perhaps my issue is here?
                "Auth":session.token
            } 
        };

    } else { // user has not uploaded an image - standard form submit 

        data.user = $scope.user; 
        data.performer = $scope.performer;
        header = { headers:{"Auth":$localStorage.session.token} };
    }

    // send request
    $http.post($rootScope.API_ROOT+'/users/'+id, data.user, header).then(function(res){ 

        console.log(res.data);

    },function(err){ console.log(err); }); // "413 (Request Entity too Large)"

}

我尝试过设置LimitRequestBody,但这并没有解决问题。

我可以在我的应用的其他部分上传类似或大尺寸的图像,其中图像数据未使用POST数据进行序列化,因此我觉得我需要更好地配置我的请求。

0 个答案:

没有答案