Ticket T1284152
Visible to All Users

(Unique) Naming images uploaded from HTMLEditor

created 4 days ago

Hi, I am using DevExtreme for ASP.NET Core and the HtmlEditor. I want to allow the user to upload images. For this I use :


x.AddSimpleFor(v => v.Content)
.Editor(e => e
.HtmlEditor()
.CustomizeModules("CustomizeHtmlEditor")
.ID("edit-html-editor")
.ImageUpload(imageUpload =>
{
imageUpload.Tabs(
tabs =>
{
tabs.Add().Name(HtmlEditorImageUploadTab.File);
tabs.Add().Name(HtmlEditorImageUploadTab.Url);
}
);
imageUpload.FileUploadMode(HtmlEditorImageUploadFileUploadMode.Server);
imageUpload.UploadUrl("/imager/upload");
imageUpload.UploadDirectory("/uploaded-images/");
imageUpload.FileUploaderOptions(x => x.MaxFileSize(10 * 1024 * 1024));
})

I have written a controller ImagerController and the POST endpoint upload. I receive the data of the image in this controller. So far everything is okay.

I have to make sure that the uploaded image has a unique name, otherwise the image of a user with the same name might be overwritten (or used if not overwritten). The html editor must receive and use the new name. To do this, my controller creates a new unique name for the image and returns the path of the image.
Unfortunately, the HTML editor requires the original name.
Question: how can I make the html editor accept the new name?

It seems a "bit impractical" to expect the user to use a globally unique name for all his images. I also don't want to store the images in a database.

Thanks for your help!

BR V

Comments (1)
Alisher (DevExpress Support) 3 days ago

    Hi,

    The HTML Editor component does not allow you to use the name of the file returned from the server. I am currently researching if this can be implemented in a custom solution. I will update this ticket once I have any news.

    Thanks,
    Alisher

    Answers approved by DevExpress Support

    created 2 days ago

    Hi,

    Thank you for your patience.

    I examined the internals of the HTML Editor, and I believe I found a custom solution to implement this functionality. Please follow these steps:

    1. Set the OnUploaded option through the HTML Editor's FileUploaderOptions helper method:
    JavaScript
    @(Html.DevExtreme().HtmlEditor() .ImageUpload(imageUpload => imageUpload .FileUploaderOptions(uploaderOptions => uploaderOptions.OnUploaded("fileUploaded")) .FileUploadMode(HtmlEditorImageUploadFileUploadMode.Server) .UploadUrl("your upload url")
    1. The code above will override the internal fileUploaded callback. The internal callback will no longer be called, but you will have control over the process of inserting uploaded images into the HTML Editor. This simple code shows a potential implementation for a newly assigned callback:
    JavaScript
    <script> function fileUploaded(e) { // e.component here is an instance of the HTML Editor's FileUploader component // so you need to get the instance of the HTML Editor to use insertEmbedd function const component = $("#html-editor").dxHtmlEditor("instance"); // e.request here is the result returned by your server option // url parameter must be set on the server const fileName = JSON.parse(e.request.response).url; component.insertEmbed(0, "extendedImage", { src: fileName, alt: "you can set alt here", width: "100px", height: "100px" }); } </script>
    1. Finally, update your server method to return data required to correctly process a new file name. In the following code, the url parameter is specified in the Ok response:
    C#
    [HttpPost("UploadImage")] public object UploadImage([FromForm] IFormFile file) { if (file != null && file.Length > 0) { your code here to upload an image here var imageUrl = get image new url nere return Json(new { url = imageUrl }); // url is used in fileUploaded } return BadRequest("No file uploaded."); }

    Please try this solution and let me know if it helps.

    Thanks,
    Alisher

      Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

      Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.