Skip to content

Commit 8a33d7b

Browse files
author
User Jenkins
committed
Sync with Kendo UI Professional
1 parent c31975c commit 8a33d7b

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: Editor Enable Upload of Images From File System
3+
description: Enable adding an image to the Editor by uploading it from the user's file system
4+
type: how-to
5+
page_title: Editor Create an Image Insert Tool from File System
6+
slug: editor-image-insert-tool-from-file-system
7+
tags: editor, image, insert, tool, file, system, upload
8+
ticketid: 1174115
9+
res_type: kb
10+
component: editor
11+
---
12+
13+
## Environment
14+
15+
<table>
16+
<tr>
17+
<td>Product</td>
18+
<td>Progress Kendo UI Editor</td>
19+
</tr>
20+
<tr>
21+
<td>Product Version</td>
22+
<td>2021.2.616</td>
23+
</tr>
24+
<tr>
25+
<td>Browser</td>
26+
<td> <a href="https://caniuse.com/?search=filereader">Review browser support</a></td>
27+
</tr>
28+
</table>
29+
30+
## Description
31+
32+
How can I create a simpler Image Insert Tool that enables me to upload from my file system?
33+
34+
## Solution
35+
36+
### Create a custom tool
37+
38+
1. Create the custom [tool](https://docs.telerik.com/kendo-ui/api/javascript/ui/editor/configuration/tools)
39+
1. Define its [tool.exec](https://docs.telerik.com/kendo-ui/api/javascript/ui/editor/configuration/tools.exec) function
40+
1. In the exec function create a file input element, click it programmatically and subscribe to its [change event](https://api.jquery.com/change/)
41+
1. (Optional) Use CSS to style the custom **Insert Image From File System** tool with the desired [Kendo Icon](https://docs.telerik.com/kendo-ui/styles-and-layout/icons-web#list-of-font-icons)
42+
43+
### Handle the change event of the file input
44+
45+
1. If there are files in the input create a new [FileReader](https://developer.mozilla.org/en-US/docs/Web/API/FileReader)
46+
1. Subscribe to its [onload event](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/onload)
47+
1. Read the contents of the file with the [FileReader.readAsDataURL()](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL) This also triggers the onload event.
48+
1. Get ready to remove the file input to prevent memory leaks
49+
50+
### Handle the onload event of the FileReader
51+
52+
1. Get the Editor's data
53+
1. Get the Base64-encoded data of the image from the content of the event
54+
1. Create a new img element and set the image data as its source
55+
1. Use the Editor's [paste method](https://docs.telerik.com/kendo-ui/api/javascript/ui/editor/methods/paste) to insert the img element into its editable area
56+
1. Remove the event handler from the file input element and then delete it from the DOM
57+
58+
59+
60+
```dojo
61+
62+
<div id="example">
63+
64+
65+
<textarea id="editor" rows="10" cols="30" style="width:100%;height:400px">
66+
67+
</textarea>
68+
69+
</div>
70+
<script>
71+
$("#editor").kendoEditor({
72+
tools: [
73+
"bold",
74+
"insertImage",
75+
{
76+
name: "myInsertImage",
77+
tooltip: "Insert an image",
78+
exec: function(e) {
79+
80+
81+
82+
var uploadInput = $("<input type='file' />");
83+
uploadInput.click();
84+
uploadInput.on("change", uploadInputChange);
85+
86+
87+
}
88+
}
89+
]
90+
});
91+
92+
function uploadInputChange (ev) {
93+
if (this.files && this.files[0]) {
94+
var reader = new FileReader();
95+
96+
reader.onload = imageIsLoaded;
97+
reader.readAsDataURL(this.files[0]);
98+
window.UPLOAD_TO_DESTROY = this;
99+
}
100+
}
101+
102+
function imageIsLoaded (ev) {
103+
var editor = $("#editor").data("kendoEditor");
104+
var base64Src = ev.target.result;
105+
var img = $("<img src='" + base64Src + "' />")[0];
106+
107+
editor.paste(img.outerHTML);
108+
img.remove();
109+
110+
if (UPLOAD_TO_DESTROY) {
111+
$(UPLOAD_TO_DESTROY).off("change");
112+
$(UPLOAD_TO_DESTROY).remove();
113+
delete UPLOAD_TO_DESTROY;
114+
}
115+
}
116+
</script>
117+
<style>
118+
.k-i-my-insert-image:before {
119+
content: "\E501"
120+
}
121+
</style>
122+
123+
```
124+
## Notes
125+
This article displays a general approach for adding an image from the user's file system in the Editor. There are further aspects of this functionality to be handled like validation of files, security issues, etc.
126+
127+
## See Also
128+
129+
[How to make a simple image upload using Javascript/Html StackOverflow article](https://stackoverflow.com/questions/22087076/how-to-make-a-simple-image-upload-using-javascript-html)

0 commit comments

Comments
 (0)