图片上传代码点火器

时间:2017-08-30 07:00:49

标签: php ajax codeigniter croppie

我正在使用croppie.js裁剪我的图像并将其发送到服务器。但有时服务器显示大图像的413错误。是否有任何其他方式将图像作为图像发送使用64基本字符串。因为croppie.js的响应是64个基本字符串。

这是我的jQuery代码段。

$('.upload-result').on('click', function (ev) {
            var file = $('input[name=userfile]').val();
            var uploadErrors = [];

            if (file === "") {
                $('.errorImgUpload').html('You did not select a file to upload').fadeIn();
                uploadErrors.push("no file");
            } else{
                $('.errorImgUpload').fadeOut();
            }

            if (uploadErrors.length === 0) {

                $('#doUpload span').html("Uploading...");

                $uploadCrop.croppie('result', {
                    type: 'canvas',
                    size: {
                        width: 760,
                        height: 860
                    }
                }).then(function (resp) {

                    var formData = new FormData($('#product_detail_images_form')[0]);

                    $.ajax({
                        url: "<?= base_url("admin_jsessID/upload_tmp/".$id); ?>",
                        type: "POST",
                        data:formData,
                        mimeType: "multipart/form-data",
                        contentType: false,
                        cache: false,
                        processData: false,
                        success: function (data) {

                            data=data.trim();
                            if (data === "1") {
                                $('input[name=userfile]').val("");

                                $uploadCrop.croppie('bind', {

                                    url: ""

                                });

                                load_uploaded_images();

                                $('.doneImgUpload').html('Image Successfully Uploaded.').fadeIn();
                                $('html, body').animate({
                                    scrollTop: $(".doneImgUpload").height()-20
                                }, 200);
                                setTimeout(function () {
                                    $('.doneImgUpload').fadeOut();
                                }, 5000);
                            }else if (data === "2"){
                                $('.errorImgUpload').html('Image Upload Failed.').fadeIn();
                                $('input[name=userfile]').val("");
                                $('html, body').animate({
                                    scrollTop: $(".errorImgUpload").height()-20
                                }, 200);
                                setTimeout(function () {
                                    $('.errorImgUpload').fadeOut();
                                }, 5000);
                            }else if (data === "3"){
                                $('.errorImgUpload').html('Image Upload Failed.').fadeIn();
                                $('input[name=userfile]').val("");
                                $('html, body').animate({
                                    scrollTop: $(".errorImgUpload").height()-20
                                }, 200);
                                setTimeout(function () {
                                    $('.errorImgUpload').fadeOut();
                                }, 5000);
                            }
                            else if (data === "0"){
                                $('.errorImgUpload').html('Upload Image Limit Exceeded.').fadeIn();
                                $('input[name=userfile]').val("");
                                $('html, body').animate({
                                    scrollTop: $(".errorImgUpload").height()-20
                                }, 200);
                                setTimeout(function () {
                                    $('.errorImgUpload').fadeOut();
                                }, 5000);
                            }

                            $('#doUpload span').html("Upload Image");

                        }
                    });
                });
            }

        });

1 个答案:

答案 0 :(得分:0)

使用压缩方法减小大小。 检查lzma-js库,它将比原始base64图像字符串减少70%。

相关问题