Issue #2910Opened July 21, 2020by SintoDema0 reactions

Issue in Asset manager handling

Question

Hi @artf,

I'm now working on grapesJS implementation in our application. I have gone through documents provided & some of the issues reported regarding the asset manager(https://github.com/artf/grapesjs/issues/216). I was not able to show the list of uploaded images in the Asset manager after a hard refresh list disappears.

I have tried to upload files to AmazonS3, upload was ok & I get its response, also it is showing in the list. In this case, I was not able to edit images due to the CORS issue.

Later, I tried with the base64 encoded image. With this method, I was able to edit uploaded images. Also got it in the image listing. As I said earlier, the image list only available until a hard refresh. I think it is from the cache.

I have doubts in a few codes, can you help me out of it.

Here is the assetManager section code:

assetManager: {
    storageType : '',
    storeOnChange : true,
    storeAfterUpload : true,
    embedAsBase64 : true, // Make this false to upload image to AmazonS3
    upload : siteURL+'assets/CONTENT/img', // For temporary storage, Upload endpoint, set `false` to disable upload
    assets : [],
    // headers: {}, // Custom headers to pass with the upload request
    // params: {}, // Custom parameters to pass with the upload request, eg. csrf token
    credentials: 'include', // The credentials setting for the upload request, eg. 'include', 'omit'
    autoAdd : 1, // If true, tries to add automatically uploaded assets.
    dropzone : 0, // Enable an upload dropzone on the entire editor (not document) when dragging files over it
    openAssetsOnDrop : 1, // Open the asset manager once files are been dropped via the dropzone
    multiUpload : true, // Allow uploading multiple files per request. If disabled filename will not have '[]' appended
    showUrlInput: true, // Toggles visiblity of assets url input
    uploadFile: function(e) {
        // Ref: https://blog.webnersolutions.com/adding-image-upload-feature-in-grapesjs-editor/
        var files = e.dataTransfer ? e.dataTransfer.files : e.target.files;
        var formData = new FormData();
        for(var i in files){
            formData.append('file-'+i, files[i]); // Containing all the selected images from local
        }
        $.ajax({
            url: siteURL + 'uploadImage_base64', // Save image as base64 encoded - Its a function
            // url: siteURL + 'uploadImage', // Upload image to AmazonS3 - Its a function
            type: 'POST',
            data: formData,
            contentType: false,
            crossDomain: true,
            dataType: 'json',
            mimeType: "multipart/form-data",
            processData: false,
            success: function(result) {
                var myJSON = [];
                if ((typeof(result['data']) != 'undefined') && (result != 'null')) {
                    $.each(result['data'], function(key, value) {
                        myJSON[key] = value;
                    });
                   console.log(myJSON);
                   // while using base64 encode => 0: {name: "ipsumImage.png", type: "image", src: "data:image/png;base64,iVBORw0KGgAAVwA…AAAAAAAAAAAAAAAD4Pv4B6rBPej6tvioAAAAASUVORK5CYII=", height: 145, width: 348}
                   // while using AmazonS3 => 0: {name: "logo_sigclub.png", type: "image", src: "https://amazonaws.com/assets/CONTENT/img/logo_sigclub.png", status: true, message: "Uploaded successfully", …}
                    editor.AssetManager.add(myJSON); //adding images to asset manager of GrapesJS
                }
            }
        });
    }
}

I have doubt in:

  1. `upload : siteURL+'assets/CONTENT/img', is this image upload path or a function which uploads image?
  2. If I'm using AmazonS3, can I able to get the editing option without the CORS issue?
  3. How can I show the uploaded images in Asset Manager without disappearing later?

Thank you.

Answers (3)

artfAugust 5, 20200 reactions

`upload : siteURL+'assets/CONTENT/img', is this image upload path or a function which uploads image?

Image upload path

If I'm using AmazonS3, can I able to get the editing option with the CORS issue?

If you're trying to upload directly to S3 from the browser (without having your endpoint as a proxy) you can't do it (this is why CORS exists). So, send the file to your endpoint, then from you backend script you can do whatever you want

How can I show the uploaded images in Asset Manager without disappearing later?

Load them from your storage and add them to the asset manager via API

SintoDemaAugust 5, 20200 reactions

Hi @artf ,

Thank you for your reply.

If you're trying to upload directly to S3 from the browser (without having your endpoint as a proxy) you can't do it (this is why CORS exists). So, send the file to your endpoint, then from your backend script you can do whatever you want.

I'm uploading the file from Asset Manager to a temporary folder from where we are moving that to Amazon S3. File uploading is working fine with Amazon S3 and is listed in the Asset manager, but CORS is showing on the time we are editing that image.

As you see in my code(console.log() is added just before I'm trying to add it to Asset), the response is getting to me. Is that the proper way I'm trying to add the image to Asset? If it's fine, then why the CORS is showing at the time of editing the image(image URL will be from the Amazon S3). We are trying to edit image from other domain(Amazon S3), is there any issue on that, will it be solved by using your API?

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.