裁剪器js中的replace()

时间:2018-06-30 11:03:17

标签: javascript ajax laravel-5.5 cropper

我的项目是保存裁切后的图像并在视图中显示。

当我裁剪图像时,它以我的形式工作,但是当我想更改图像时,我再次裁剪并保存。它创建具有相同图像的两行。 当我将图像更改3次时,它会用相同的图像创建3行,依此类推。

有一个我必须使用的方法称为replace(),但我不知道如何使用它。

这是我的代码

 window.addEventListener('DOMContentLoaded', function () {
        var avatar = document.getElementById('avatar');
        var image = document.getElementById('image');
        var input = document.getElementById('input');
        var $progress = $('.progress');
        var $progressBar = $('.progress-bar');
        var $alert = $('.alert');
        var $modal = $('#modal');
        var cropper;


        var title = $('#title');
        var description = $('#description');
        var arabic_title = $('#arabic_title');
        var arabic_description = $('#arabic_description');

        $('[data-toggle="tooltip"]').tooltip();

        input.addEventListener('change', function (e) {
            var files = e.target.files;
            var done = function (url) {
                input.value = '';
                image.src = url;
             //  $alert.hide();
                $modal.modal('show');
            };
            var reader;
            var file;
            var url;

            if (files && files.length > 0) {
                file = files[0];

                if (FileReader) {
                    reader = new FileReader();
                    reader.onload = function (e) {
                        done(reader.result);
                        console.log('ok2');
                    };
                    reader.readAsDataURL(file);
                    console.log('ok3');


                }
            }


        });


        $modal.on('shown.bs.modal', function () {
            cropper = new Cropper(image, {
                aspectRatio: 1.7,
                viewMode: 3,
            });

        }).on('hidden.bs.modal', function () {
            cropper.destroy();
            cropper = null;
        });

        document.getElementById('crop').addEventListener('click', function () {
            var initialAvatarURL;
            var canvas;

            $modal.modal('hide');

            if (cropper) {
                canvas = cropper.getCroppedCanvas({
                    width: 400,
                    height: 400,
                });

                initialAvatarURL = avatar.src;
                avatar.src = canvas.toDataURL();

                $progress.show();
                $alert.removeClass('alert-success alert-warning');

                document.getElementById('save').addEventListener('click', function () {

                    canvas.toBlob(function (blob) {
                        var formData = new FormData();

                        formData.append('avatar', blob);
                        formData.append('title', title.val());
                        formData.append('description', description.val());
                        formData.append('arabic_title', arabic_title.val());
                        formData.append('arabic_description', arabic_description.val());



                        if (title.val() !== '' && description.val() !== '' && arabic_title.val() !== '' && arabic_description.val() !== '') {


                        for (let pair of formData.entries()) {
                            console.log(pair[0] + ', ' + pair[1]);

                        }
                            $.ajaxSetup({
                                headers: {
                                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                }
                            });

                            $.ajax("{{url('admin/services')}}", {
                                method: 'POST',
                                data: formData,
                                processData: false,
                                contentType: false,

                                xhr: function () {
                                    var xhr = new XMLHttpRequest();

                                    xhr.upload.onprogress = function (e) {
                                        var percent = '0';
                                        var percentage = '0%';

                                        if (e.lengthComputable) {
                                            percent = Math.round((e.loaded / e.total) * 100);
                                            percentage = percent + '%';
                                            $progressBar.width(percentage).attr('aria-valuenow', percent).text(percentage);
                                        }
                                    };

                                    return xhr;
                                },

                                success: function (data) {
                                    $alert.show().addClass('alert-success').text('Upload success');
                                    console.log(data);

                                },

                                error: function (error) {
                                    avatar.src = initialAvatarURL;
                                    $alert.show().addClass('alert-warning').text('Upload error');
                                    console.log(error);
                                },

                                complete: function () {
                                    $progress.hide();
                                },
                            });
                        }


                    });


                });
            }
        });
    });

2 个答案:

答案 0 :(得分:0)

$service = 'No service';
    if (isset($_FILES['img'])) {

            $service = Service::create(['title'=>$request->title,
                'description'=>$request->description,
            'photo'=>$request->img]);

        }
        return $service;

尝试一下。

答案 1 :(得分:0)

您的表格应该是

<form action="files/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="photo"/>
</form

您的控制器应该像

if ($request->hasFile('photo')) {
    // move file upload here
}