[QUESTION] How do I save the images to database?
Please do bare with me but it's really hard to understand the documentation. Can you guys help me do do this?
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 thanks bro. I'll try doing that.
@JulyanoF I am getting unexpected identifier in autoAdd: true
@pranzikkin it’s missing a comma in line above, I edited...
@JulyanoF what would be the name of the file when it's passed to the php? // $_FILES['whatsthename'];
@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
@pranzikkin the name of the input is file. So you should access something like $_FILES['file'].
Try that and let me know
@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.
@arthuralmeidap trying it right now. Thanks for the help
@pranzikkin You're welcome, I'm implementing GrapesJS in my CakePHP 3.x project 👍
@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
@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 do you know how to save the template you created in a db? and on how to load it back into the editor?
@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 😮
@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 okay bro I'll check it. what does the setStepsBeforeSave mean?
@pranzikkin API Reference - Storage Manager
@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
@pranzikkin Stack Overflow In savetodb.php you must to return a json object
@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