Articles in this section
Category / Section

How to Achieve File Name for Uploaded Image in Blazor RichTextEditor?

To achieve unique file names for uploaded images in the Blazor Rich Text Editor, you can utilize the SaveUrl configuration and the OnImageUploadSuccess event. This allows you to rename the uploaded image files by appending a GUID to the file name.

Implementation Steps

1. Configure the RichTextEditor

In your Razor component, set up the RichTextEditor with the necessary event handler and image settings:

<SfRichTextEditor>
    <RichTextEditorEvents OnImageUploadSuccess="@ImageUploadSuccess"></RichTextEditorEvents>
    <RichTextEditorImageSettings SaveUrl="api/Home/Save" Path="./Images/" />
</SfRichTextEditor>

@code{  
    public string[] header { get; set; }
    
    private void ImageUploadSuccess(ImageSuccessEventArgs args)
    {
        var headers = args.Response.Headers.ToString();
        header = headers.Split("name: ");
        header = header[1].Split("\r");

        // Update the modified image name to display an image in the editor.
        args.File.Name = header[0];
    }
}

2. Create the Save Endpoint

In your controller, implement the endpoint that handles the image upload. This example demonstrates how to rename the uploaded image file by appending a GUID:

[HttpPost("[action]")]
[Route("api/Home/Save")]
public void Save(IList<IFormFile> UploadFiles)
{
    try
    {
        foreach (IFormFile file in UploadFiles)
        {
            if (UploadFiles != null)
            {
                string targetPath = hostingEnv.ContentRootPath + "\\wwwroot\\Images";
                string filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

                // Create a new directory if it does not exist
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                string guid = Guid.NewGuid().ToString();
                string newFileName = $"rteImage-{guid}-{filename}";
                string path = Path.Combine(hostingEnv.WebRootPath, "Images", newFileName);

                if (!System.IO.File.Exists(path))
                {
                    using (FileStream fs = System.IO.File.Create(path))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }

                    // Send the modified file name through response header
                    Response.Headers.Add("name", newFileName);
                    Response.StatusCode = 200;
                    Response.ContentType = "application/json; charset=utf-8";
                }
            }
        }
    }
    catch (Exception e)
    {
        Response.Clear();
        Response.ContentType = "application/json; charset=utf-8";
        Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
    }
}

Key Points

  • The OnImageUploadSuccess event allows you to modify the file name after the upload is successful.
  • A GUID is generated for each uploaded image to ensure uniqueness.
  • The modified file name is sent back in the response header, which can be used to display the image in the editor.

Additional References

Conclusion
I hope you enjoyed learning how to achieve file name for uploaded image in Blazor RichTextEditor.
You can refer to our Blazor Rich Text Editor feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor Rich Text Editor example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied