javascript - 通过 file_put_contents 将 Base64 图像发布到文件夹时,ajax 出现 413 Request Entity Too Large 错误

标签 javascript php jquery ajax codeigniter

我在通过 ajax 发送数据时收到 413 Request Entity Too Large 错误 我正在转换 Base64 图像编码代码...我将其传递给 我的 Controller 在 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...');    
                    }
                });


});

我的codeigniter Controller 功能

    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;
    }
}

将其增加到更合理的数字并进行更多测试。

关于javascript - 通过 file_put_contents 将 Base64 图像发布到文件夹时,ajax 出现 413 Request Entity Too Large 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24837033/

相关文章:

javascript - 我正在尝试将选择选项值与常数相乘

PHP 和类 : access to parent's public property within the parent class

php - 如何使用GD检查PNG图像是否具有透明度?

javascript - scrollTop() 不起作用

javascript - 在移动应用程序中包含 moment.js

javascript - Chrome 扩展程序 "Receiving end does not exist."错误

javascript - 模板中不需要 $parent 绑定(bind)

php - .htaccess 文件更改 url 字符串在某些页面上不起作用

javascript - Html下拉: how to handle key events of select option element

javascript - 如何使用控制台日志到 ajax 参数?