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:
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