We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Rename images before inserting it in RichTextEditor

Uncaught ReferenceError: args is not definedI am trying to rename the uploaded image in order to avoid duplicate names. 

I tried to follow https://ej2.syncfusion.com/aspnetcore/documentation/rich-text-editor/how-to/rename-images-in-server/

But when I do, it doesn't work. 

The following are part of the cshtml:

@using Syncfusion.JavaScript;
@using Syncfusion.JavaScript.Models;
...

<form asp-controller="Classification" asp-action="Create" class="mt-3" method="post">

...
   <ejs-richtexteditor ejs-for="@Model.Classification.Description" id="defaultRTE"imageUploadSuccess="onImageUploadSuccess">
        <e-richtexteditor-insertimagesettings saveUrl="/Classification/SaveImage" path="Images/Content/"></e-richtexteditor-insertimagesettings>
        <e-richtexteditor-iframesettings enable="true"></e-richtexteditor-iframesettings>
        <e-richtexteditor-quicktoolbarsettings image="@ViewBag.image"></e-richtexteditor-quicktoolbarsettings>

        <e-richtexteditor-toolbarsettings items="@ViewBag.tools"></e-richtexteditor-toolbarsettings>
        <e-content-template>

        </e-content-template>
    </ejs-richtexteditor>
  

...
<script>
    function onImageUploadSuccess() {
        alert("hi");
        if (args.e.currentTarget.getResponseHeader('name') != null) {
            alert("hi2");
            args.file.name = args.e.currentTarget.getResponseHeader('name');
            var filename = document.querySelectorAll(".e-file-name")[0];
            filename.innerHTML = args.file.name.replace(document.querySelectorAll(".e-file-type")[0].innerHTML, '');
            filename.title = args.file.name;
        }
    }
</script>

on the controller I have the following code:

        [HttpPost]
        public ActionResult SaveImage(IList<IFormFile> UploadFiles)
        {
            try
            {
                foreach (IFormFile file in UploadFiles)
                {
                    if (UploadFiles != null)
                    {
                        string filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        Guid g = Guid.NewGuid();
                        string GuidString = Convert.ToBase64String(g.ToByteArray());
                        GuidString = GuidString.Replace("=", "");
                        GuidString = GuidString.Replace("+", "");
                        string onlyfilename = GuidString + $@"{filename}";
                        filename = hostingEnv.ContentRootPath + "\\wwwroot\\Images\\Content\\" + GuidString + $@"{filename}";
                        if (!System.IO.File.Exists(filename))
                        {
                            Response.ContentType = "application/json; charset=utf-8";
                            Response.Headers.Add("name", onlyfilename);
                            //Response.Clear();
                            Response.StatusCode = 204;
                            
                            using (FileStream fs = System.IO.File.Create(filename))
                            {
                                file.CopyTo(fs);
                                fs.Flush();
                            }
                            //Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists.";
                        }
                        else
                        {
                            Response.ContentType = "application/json; charset=utf-8";
                            Response.Headers.Add("name", onlyfilename);
                            Response.Clear();
                            Response.StatusCode = 204;
                            Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists.";
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Response.Clear();
                Response.ContentType = "application/json; charset=utf-8";
                Response.StatusCode = 204;
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "No Content";
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
            }
            return Content("");
        }
    }

I checked and the developer tools of chrome. There I have the response header as:

  1. content-type:
    application/json; charset=utf-8
  2. date:
    Fri, 10 Jan 2020 05:47:06 GMT
  3. name:
    U5XajulQW0WDwOV4xaP7pgSample10.jpg
  4. server:
    Microsoft-IIS/10.0
  5. status:
    204
  6. x-powered-by:
    ASP.NET

But I also see in the console an error:
Uncaught ReferenceError: args is not defined

Any suggestions how to get this working. I am very new to a lot of the related technologies though.

Thanks,

Peter

3 Replies

IS Indrajith Srinivasan Syncfusion Team January 13, 2020 05:23 AM UTC

Hi Peter,

Greetings from Syncfusion support
 
 
We have validated your query and reproduced it from our end. The issue is due to not received the additional event arguments in the imageUploadSuccess event. We have prepared a sample based on your requirement and also we will correct the documentation from our end and refresh the documentation by Jan 14th .

API: https://help.syncfusion.com/cr/aspnetcore-js2/Syncfusion.EJ2.RichTextEditor.RichTextEditor.html#Syncfusion_EJ2_RichTextEditor_RichTextEditor_ImageUploadSuccess
 
 
 
function onImageUploadSuccess(args) { 
      …. 
} 
 

Sample for reference:- https://www.syncfusion.com/downloads/support/forum/150534/ze/RTEServerImage887505175

Could you  please try out the above solution and let us know is you face any difficulties.

Regards,
 
Indrajith S 



PE Peter January 14, 2020 04:48 AM UTC

Hi Indrajith

Thank you very much for your help. It is working now.

Kind regards,

Peter


IS Indrajith Srinivasan Syncfusion Team January 15, 2020 04:49 AM UTC

Hi Peter,  
  
Thanks for your update. Please get back to us if you need any further assistance on this. 
  
Regards,  
Indrajith 


Loader.
Live Chat Icon For mobile
Up arrow icon