当通过file_put_contents将base64图像发送到文件夹时,从ajax获得413请求实体太大错误


getting 413 Request Entity Too Large error from ajax when posting base64 image to folder by file_put_contents

我得到413请求实体太大的错误,而我通过ajax发送我的数据我正在转换base64图像编码代码…我把它传给我的控制器功能在codeigniter.....413请求实体太大

我的ajax代码如下

    $('#save-image-php').click(function() {
        $.ajax({
                    type: "POST",              
                    url: php_directory_save,       
                    data: { base64_image: yourDesigner.getProductDataURL()},
                    success: function(data) {
                         if(parseInt(data) > 0) {
                            $( "#cart_pd" ).submit();
                            }
                    },
                    error: function() {
                        //alert('some error has occured...');
                    },
                    start: function() {
                        //alert('ajax has been started...');    
                    }
                });

});

my codeigniter controller function

    public function saveimage(){

        $base64_str = substr($this->input->post('base64_image'),strpos($this->input->post('base64_image'), ",")+1);
        $decoded = '';
        $decoded = base64_decode($base64_str);
        $png_name = '';
        $png_name = "product-".strtotime('now').".png";
        $png_url = '';
        $png_url = "uploaded_files/custom_image/".$png_name;

         $id = $this->product_model->save($png_name);
         $data = array('cust_id'=>$id); 
         $this->session->set_userdata($data);
         $result  = '';
         $result = file_put_contents($png_url, $decoded);
        if($id !=''){
            header('Content-Type: application/json');
            echo json_encode($id);
                   }
    }   

如果你在nginx上运行,这是可能的原因

nginx.conf

http {
    server {
        client_max_body_size 20M;
    }
}

增加到更合理的数字,并做更多的测试。