T
T
Tat622019-10-23 11:08:27
ASP.NET
Tat62, 2019-10-23 11:08:27

How to pass cropper result (cropped image) to controller?

There is a cropper:

var image = document.getElementById('cropImage');
                    if (cropper) { cropper.destroy(); }
                    cropper = new Cropper(image, {
                        aspectRatio: 268 / 249,
                        dragMode: 'none',
                        autoCropArea: 1,
                        movable: false,
                        rotatable: false,
                        scalable: false,
                        zoomable: false,
                        background: false,
                        viewMode: 3
                    })

Its result: How to pass it to the controller. Tried formData.append("image", cropData);
var cropData = cropper.getData(true);

var formData = new FormData();
                    formData.append("image", files[0]);
                    formData.append("image", cropData);
                    var MyAppUrlSettings = {
                        MyUsefulUrl: '@Url.Action("ImageCreate", "Home")'
                    }
                    var request = new XMLHttpRequest();
 
                    request.open('POST', MyAppUrlSettings.MyUsefulUrl);
                    request.onload = function(e) {
                    if (requestreadyState === 4) {
                    if (request.status === 200) {
                            // тут установить src для картинки
                             } else {
                                 console.error(request.statusText);
                       }
                       }
                };
                    request.send(formData);

Does not work. The controller receives null, i.e. crop=null
ActionResult ImageCreate(HttpPostedFileBase image, HttpPostedFileBase crop)
        {
            ...................................................
        }

Can anyone tell me why this is happening and what needs to be done. Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ascar, 2019-10-24
@Ascar

Check that the data is being transferred, if not, then check for the presence of multipart/form-data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question