grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

[QUESTION] How do I save the images to database?

Open josefph opened this issue 8 years ago • 60 comments

Please do bare with me but it's really hard to understand the documentation. Can you guys help me do do this?

josefph avatar Feb 05 '18 09:02 josefph

Hello @pranzikkin You can pass images to your backend to proccess image and store it in database:

var editor = grapesjs.init({
    container : '#gjs',
    assetManager: {
        upload: 'http://localhost/mybackend/storeImageDatabase.php',
        autoAdd: true
    },
    // [...]
});

And in PHP file you can take the image as a file: $_FILES

JulyanoF avatar Feb 05 '18 14:02 JulyanoF

@JulyanoF thanks bro. I'll try doing that.

josefph avatar Feb 06 '18 02:02 josefph

@JulyanoF I am getting unexpected identifier in autoAdd: true

josefph avatar Feb 06 '18 02:02 josefph

@pranzikkin it’s missing a comma in line above, I edited...

JulyanoF avatar Feb 06 '18 11:02 JulyanoF

@JulyanoF what would be the name of the file when it's passed to the php? // $_FILES['whatsthename'];

josefph avatar Feb 06 '18 12:02 josefph

@pranzikkin it is an array of files: $_FILES['files'] You can debug and see the response on browser's network (console). As it is passing a single file of each time, the file is passing at the first position, so you can take the name of file ($_FILES['files']['name'][0]) the tmp name ($_FILES['files']['tmp_name'][0])... Reference: PHP - File Upload

JulyanoF avatar Feb 06 '18 12:02 JulyanoF

@pranzikkin the name of the input is file. So you should access something like $_FILES['file'].

Try that and let me know

arthuralmeidap avatar Feb 06 '18 12:02 arthuralmeidap

@JulyanoF okay I'll try it. thanks for the great help. and at the same time sorry for the noobish questions. I'm new to using a framework that uses js. I am actually used to PHP and laravel framework.

josefph avatar Feb 06 '18 12:02 josefph

@arthuralmeidap trying it right now. Thanks for the help

josefph avatar Feb 06 '18 12:02 josefph

@pranzikkin You're welcome, I'm implementing GrapesJS in my CakePHP 3.x project 👍

JulyanoF avatar Feb 06 '18 12:02 JulyanoF

@JulyanoF would it be okay to ask you to help me with future problems that I will encounter while implementing grapesjs in my laravel project? I will just gonna ask some questions. not literally need you to help me build my project lol

josefph avatar Feb 06 '18 12:02 josefph

@pranzikkin I'm trying to learn this framework at all too.. I'm having many difficulties too hahaha. You can see how many questions I made here :/

JulyanoF avatar Feb 06 '18 12:02 JulyanoF

@JulyanoF do you know how to save the template you created in a db? and on how to load it back into the editor?

josefph avatar Feb 06 '18 12:02 josefph

@pranzikkin yes, I made exactly it hahaha and I created tree differente "Models" to user choose to start with them too xD And I made very significative changes in editor 😮

JulyanoF avatar Feb 06 '18 13:02 JulyanoF

@pranzikkin check it (it's a option of init method):

storageManager: {
        autosave: true,
        setStepsBeforeSave: 2,
        type: 'remote',
        urlStore: 'http://localhost/store.php',
        urlLoad: 'http://localhost/load.php',
        contentTypeJson: true,
    },

JulyanoF avatar Feb 06 '18 13:02 JulyanoF

@JulyanoF okay bro I'll check it. what does the setStepsBeforeSave mean?

josefph avatar Feb 06 '18 13:02 josefph

@pranzikkin API Reference - Storage Manager

JulyanoF avatar Feb 06 '18 13:02 JulyanoF

@JulyanoF I'm getting this error while trying to save an image. Failed to load http://localhost/grapesjs-dev/savetodb.php: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

and I tried adding header('Access-Control-Allow-Origin: *'); on top of the php file. still no luck

josefph avatar Feb 06 '18 13:02 josefph

@pranzikkin Stack Overflow In savetodb.php you must to return a json object

JulyanoF avatar Feb 06 '18 13:02 JulyanoF

@pranzikkin , This error is happening because the grapes is probably running on a different port from the server. Something like localhost:8080 and the server runs on 80. as @JulyanoF mention you can enable the CORS for this

arthuralmeidap avatar Feb 06 '18 13:02 arthuralmeidap