New to Telerik UI for ASP.NET Core? Download free 30-day trial

Image Browser

By default, the Insert Image tool opens a simple dialog that allows you to type in or paste the URL of an image and, optionally, specify a tooltip.

The Editor also supports another way of picking an image by browsing a list of predefined files and directories. Uploading new images is also supported. For a runnable example, refer to the demo on file and image browser in the Editor.

To retrieve and upload the files and directories, the image browser needs a server-side implementation. To configure the image browser tool, use the ImageBrowser() method.

@(Html.Kendo().Editor()
    .Name("editor")
    .ImageBrowser(imageBrowser => imageBrowser
        .Image("~/Content/UserFiles/Images/{0}")
        .Read("Read", "ImageBrowser")
        .Create("Create", "ImageBrowser")
        .Destroy("Destroy", "ImageBrowser")
        .Upload("Upload", "ImageBrowser")
        .Thumbnail("Thumbnail", "ImageBrowser")
    )
)

The following list provides information about the default requests and responses for the Create(), Read(), Destroy(), and Upload() operations.

  • Create()—Makes a POST request for the creation of a directory with the following parameters and does not expect a response.

    { "name": "New folder name", "type": "d", "path": "foo/" }
    
  • Read()—Makes a POST request that contains the path parameter which specifies the path that is browsed and expects a file listing in the following format.

    [
        { "name": "foo.png", "type": "f", "size": 73289 },
        { "name": "bar.jpg", "type": "f", "size": 15289 },
        ...
    ]
    

    name from the previous example is the name of the file or directory, type is either an f for a file or a d for a directory, and size is the file size (optional).

  • Destroy()—Makes a POST request with the following parameters:

    • name—The file or the directory that will be deleted.
    • path—The directory in which the file or the directory resides.
    • type—The file or the directory that will be deleted (an f or a d).
    • size—(Optional) The file size, as provided by the Read() response.
  • Upload()—Makes a POST request. The request includes FormData which contains the upload path and the file name and type. Its payload consists of the uploaded file. The expected response is a file object in the following format:

    { "name": "foo.png", "type": "f", "size": 12345 }
    
  • Image()—Used by the Editor to generate the src attribute of the original image when the image is inserted. It results in a GET request generated by the browser for each inserted image. The URL can point to a file system or to a service and is parameterized—the {0} placeholder denotes the path and fileName as received from the Read() service.

You can update all of these requests and responses through the ImageBrowser() configuration. Each of them exposes more options that you can tweak.

ImageBrowser in Razor Page scenario

In order to set up the ImageBrowser of the Telerik UI Editor HtmlHelper for ASP.NET Core component in Razor page scenario, you need to configure the Read , Create, Update and Destroy methods of the ImageBrowser Transport configuration. The URL in these methods should refer the name of the handler in the PageModel. See the implementation details in the example below, and for the full project with RazorPages examples, visit our GitHub repository.

    @(Html.Kendo().Editor()
            .Name("editor")
            .ImageBrowser(imageBrowser => imageBrowser
                .Transport(transport => {
                    transport.Read(r => r.Url("/Editor/ImageBrowser?handler=Read"));
                    transport.Create(c => c.Url("/Editor/ImageBrowser?handler=Create"));
                    transport.Destroy(d => d.Url("/Editor/ImageBrowser?handler=Destroy"));
                    transport.UploadUrl("/Editor/ImageBrowser?handler=Upload");
                    transport.ImageUrl("/Images/{0}");

                })
                .Schema(schema => schema.Model(
                    model => model.Fields(fields => fields
                        .Name(name => name.Field("Name"))
                        .Size(size => size.Field("Size"))
                        .Type(type => type.Field("EntryType"))
                        )
                    )
                )
            )
        )
    public JsonResult OnPostRead(string path)
    {
        var fullPath = NormalizePath(path);

        if (AuthorizeRead(fullPath))
        {
            try
            {
                var files = directoryBrowser.GetFiles(fullPath, Filter);
                var directories = directoryBrowser.GetDirectories(fullPath);
                var result = files.Concat(directories);

                return new JsonResult(result.ToArray());
            }
            catch (DirectoryNotFoundException)
            {
                throw new Exception("File Not Found");
            }
        }

        throw new Exception("Forbidden");
    }

    public JsonResult OnPostCreate(string path, FileBrowserEntry entry)
    {
        var fullPath = NormalizePath(path);
        var name = entry.Name;

        if (name.HasValue() && AuthorizeCreateDirectory(fullPath, name))
        {
            var physicalPath = Path.Combine(fullPath, name);

            if (!Directory.Exists(physicalPath))
            {
                Directory.CreateDirectory(physicalPath);
            }

            return new JsonResult(entry);
        }

        throw new Exception("Forbidden");
    }

    public JsonResult OnPostDestroy(string path, FileBrowserEntry entry)
    {
        var fullPath = NormalizePath(path);

        if (entry != null)
        {
            fullPath = Path.Combine(fullPath, entry.Name);

            if (entry.EntryType == "f")
            {
                DeleteFile(fullPath);
            }
            else
            {
                DeleteDirectory(fullPath);
            }

            return new JsonResult(new object[0]);
        }

        throw new Exception("File Not Found");
    }

    public virtual ActionResult OnPostUpload(string path, IFormFile file)
    {
        var fullPath = NormalizePath(path);

        if (AuthorizeUpload(fullPath, file))
        {
            SaveFile(file, fullPath);

            var result = new FileBrowserEntry
            {
                Size = file.Length,
                Name = GetFileName(file)
            };

            return new JsonResult(result);
        }

        throw new Exception("Forbidden");
    }

See Also

In this article
Not finding the help you need? Improve this article