AlexanderKim's avatar

Storing summernote images in a database

I've done so far the following:

Ajax request:

            $('#description').summernote({
                callbacks: {
                    onImageUpload: function(files) {
                        for(var i=0; i < files.length; i++) {
                            $.upload('image', files[i]);
                        } 
                    }
                }
            });

            $.upload = function (file) {
                let out = new FormData();
                out.append('file', file, file.name);

                $.ajax({
                    method: 'POST',
                    url: '/summer-uploads',
                    //check laravel document: https://laravel.com/docs/5.6/csrf#csrf-x-csrf-token
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    contentType: false,
                    cache: false,
                    processData: false,
                    dataType: 'JSON',
                    data: out,
                    success: function (r) {
                        $('#description').summernote('insertImage', r.url);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        console.error(textStatus + " " + errorThrown);
                    }
                });
            };

Controller:

    public function summerUploads(Request $request)
    {
        if ($request->ajax()) {
            $file = $request->input('image');

            $store = $file->store('summer-uploads');

            return url() . '/summer-uploads/' . $store;
        }

        return App::abort(404);
    }

Routes:

Route::any('summer-uploads', 'AdminController@summerUploads');

If i try to upload an image, i'm getting the error: 500 (Internal Server Error) in console. If i try to in POSTMAN, i do get "The page has expired due to inactivity. " My CSRF token in meta is present, what could be an issue?

0 likes
0 replies

Please or to participate in this conversation.