calin.ionut's avatar

when uploading images with dropzone i am not redirected to home page

Hello,

I created a form and use this form for upload some images + data (in one request) - on the client side for the file upload i use dropzonejs.

I have the following conf for dropzone:

// initialize dropzone
    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone(
        "#myDropzone",
        {
            uploadMultiple: true,
            parallelUploads: 6,
            maxFiles: 6,
            maxFilesize: 2,
            addRemoveLinks: true,
            acceptedFiles: '.jpg,.jpeg,.JPEG,.JPG,.png,.PNG',
            autoProcessQueue: false,
            previewsContainer: '#dropzonePreview',
            clickable:'#dropzonePreview',
            init: function() {
                var myDropzone = this;
                //now we will submit the form when the button is clicked
                $("#companySubmit").on('click',function(e) {
                    if($('#myDropzone').valid()){
                        if(myDropzone.files.length > 0){
                            e.preventDefault();
                            myDropzone.processQueue();
                            myDropzone.on("complete", function(file) {
                                myDropzone.removeFile(file);
                                window.location.href=home;                   
                            });
                        }
                        else{
                            myDropzone.uploadFiles([]);
                        }
                    }
                });
            }
        }
    );

The images are not required, so if the form is without images then after submit AND everything is ok i get redirected to home page - which is what i want.

When i am uploding photos i think e.preventDefaults() dont let the page to be redirected. the controller return

return redirect()->route('home');

so for that i use a little hack in the config: window.location.href=home; where home is a variable in the blade file.

Is there a better way to do this? after submitting all the data (files + other data) and everything went ok, how to get redirected to home?

How to return an error from controller to the view? (for ex if the file is corrupted)

I am using laravel 5.4 and intervention image for laravel. I am quite new to laravel ... and i like this framework :)

0 likes
2 replies
tykus's avatar

You should not be returning a redirect status to an asynchronous call. One could ask why you would use AJAX if you intend to redirect.

Assuming home translates to a URI:

window.location.replace(home);

or

window.location = home;
calin.ionut's avatar

about the return error from controller....ihave tried

return response()->json([
                'error' => true,
                'message' => 'Server error while uploading',
                'code' => 500
            ], 500);

but in the event it does not catch the error:

myDropzone.on('error', function(file, response){
        var resp = JSON.parse(response);
        alert("code:" + resp.code + ", message: " + resp.message)
    });

in browser console i get the error....

Please or to participate in this conversation.