Insert Video
22 Dec 202224 minutes to read
The Rich Text Editor allows you to insert videos from online sources and local computers where you want to insert the video in your content. For inserting the video to the Rich Text Editor, the following list of options have been provided in the InsertVideoSettings.
Options | Description |
---|---|
AllowedTypes | Specifies the extensions of the video types allowed to insert on bowering and passing the extensions with comma separators. For example, pass allowedTypes as .mp4 , .mov , .wmv and .avi . |
LayoutOption | Sets the default display for a video when it is inserted into the Rich Text Editor. Possible options are: Inline and Break . |
SaveFormat | Sets the default save format of the video element when inserted. Possible options are: Blob and Base64 . |
Width | Sets the default width of the video when it is inserted in the Rich Text Editor. |
MinWidth | Sets the minWidth of the video element when it is inserted in the Rich Text Editor. |
MaxWidth | Sets the maxWidth of the video element when it is inserted in the Rich Text Editor. |
Height | Sets the default height of the video when it is inserted in the Rich Text Editor. |
MinHeight | Sets the minHeight of the video element when it is inserted in the Rich Text Editor. |
MaxHeight | Sets the maxHeight of the video element when it is inserted in the Rich Text Editor. |
SaveUrl | Provides URL to map the action result method to save the video. |
RemoveUrl | Provides URL to map the action result method to remove the video. |
Path | Specifies the location to store the video. |
Resize | Sets the resizing action for the video element. |
ResizeByPercent | Sets the percentage values for the video element with the resizing action. |
Configure video tool in the toolbar
To include the video tool in the Rich Text Editor, you can add the toolbar item Video
to the ToolbarSettings
Items property.
To configure Video
toolbar item, refer to the below code.
@Html.EJS().RichTextEditor("editor").ToolbarSettings(e => e.Items((object)ViewBag.items)).Render()
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.items = new[] { "Video" };
return View();
}
}
Insert video from web
To insert a video from the hosted link or local machine, you should enable the video tool on the editor’s toolbar. By default, the video tool opens the dialog, allowing you to insert a video as an embedded URL. You can switch to a web URL to insert the video file from the online source.
Insert from web URL
The video tool default opens the video dialog, allowing you to insert an embedded URL.
Insert from web URL
Switching the option to the web URL in the video dialog allows you to insert a video from the online source. Inserting the URL will be added to the src
attribute of the <source>
tag.
Upload and insert video
In the video dialog, by using the browse
option, select the video from the local machine and insert it into the Rich Text Editor content.
If the path field is not specified in the InsertVideoSettings, the video will be converted into Blob
url or Base64
and inserted inside the Rich Text Editor.
Restrict video upload based on size
Using the Rich Text Editor FileUploading
event, you can restrict the video to upload when the given video size is greater than the allowed fileSize. Also, the video size in the argument will be returned in bytes
.
NOTE
The file size in the argument will be returned in
bytes
.
In the following example, the video size has been validated before uploading and determined whether the video has been uploaded or not
In the following example, the video size has been validated before uploading and determined whether the video has been uploaded or not.
@Html.EJS().RichTextEditor("editor").ToolbarSettings(e => e.Items((object)ViewBag.items)).InsertVideoSettings(obj => obj.SaveUrl("https://aspnetmvc.syncfusion.com/services/api/uploadbox/Save").Path("../Files/")).FileUploading("onFileUploading").Render()
<script>
function onFileUploading(args) {
var imgSize = 500000;
var sizeInBytes = args.fileData.size;
if ( imgSize < sizeInBytes ) {
args.cancel = true;
}
}
</script>
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.items = new[] { "Video" };
return View();
}
}
Server-side action
The selected video can be uploaded to the required destination using the controller action below. Map this method name in InsertVideoSettings.SaveUrl and provide required destination path through InsertVideoSettings.Path properties.
NOTE
If you want to insert lower-sized video files in the editor and don’t want a specific physical location for saving the video, you can save the format as
Base64
.
In the following code blocks, you can insert the video files which are saved in the specified path.
@Html.EJS().RichTextEditor("defaultRTE").ToolbarSettings(e => e.Items((object)ViewBag.items)).InsertVideoSettings(obj => obj.SaveUrl("/Home/SaveFiles").Path("../Files/")).Render()
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace FileUpload.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.items = new[] { "Video" };
return View();
}
[AcceptVerbs("Post")]
public void SaveFiles(HttpPostedFileBase UploadFiles)
{
if (UploadFiles != null)
{
string path = Server.MapPath("~/Files/");
// Create a new directory, if it does not exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Save a file in directory
UploadFiles.SaveAs(path + Path.GetFileName(UploadFiles.FileName));
}
}
}
}
Video save format
The video files can be saved as Blob
or Base64
url by using the InsertVideoSettings.SaveFormat property, which is of enum type and the generated url will be set to the src
attribute of the <source>
tag.
NOTE
The default
SaveFormat
property is set toBlob
format.
By default, the files are saved in the
Blob
format.
<video>
<source src="blob:http://ej2.syncfusion.com/3ab56a6e-ec0d-490f-85a5-f0aeb0ad8879" type="video/mp4" >
</video>
<video>
<source src="data:video/mp4;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHA" type="video/mp4" >
</video>
Replacing video
Once a video file has been inserted, you can replace it using the Rich Text Editor QuickToolbarSettings videoReplace
option. You can replace the video file either by using the embedded URL or the web URL and the browse option in the video dialog.
Delete video
To remove a video from the Rich Text Editor content, select the video and click the videoRemove
tool from the quick toolbar. It will delete the video from the Rich Text Editor content as well as from the service location if the InsertVideoSettings.RemoveUrl is given.
Once you select the video from the local machine, the URL for the video will be generated. You can remove the video from the service location by clicking the cross icon.
The following example explains how to configure InsertVideoSettings.removeUrl
to remove a saved video from the remote service location when the following video remove actions are performed:
-
delete
key action. -
backspace
key action. - Removing uploaded video file from the insert video dialog.
- Deleting video using the quick toolbar
videoRemove
option.
@Html.EJS().RichTextEditor("RteImage").InsertImageSettings(obj => obj.SaveUrl("/Home/SaveImage").RemoveUrl("/Home/RemoveImage").Path("../Uploads/")).Render()
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[AcceptVerbs("Post")]
public void SaveImage(HttpPostedFileBase UploadFiles)
{
try
{
if (UploadFiles != null)
{
string fileName = UploadFiles.FileName;
var fileSave = System.Web.HttpContext.Current.Server.MapPath("~/Uploads");
// Create a new directory, if it does not exists
if (!System.IO.Directory.Exists(fileSave))
{
System.IO.Directory.CreateDirectory(fileSave);
}
var fileSavePath = Path.Combine(fileSave, fileName);
// Save the image
UploadFiles.SaveAs(fileSavePath);
Response.StatusCode = 200;
Response.ContentType = "application/json; charset=utf-8";
}
}
catch (Exception)
{
Response.StatusCode = 204;
}
}
[AcceptVerbs("Post")]
public void RemoveImage(HttpPostedFileBase UploadFiles)
{
try
{
if (UploadFiles != null)
{
// Do remove action here
Response.StatusCode = 200;
Response.ContentType = "application/json; charset=utf-8";
}
}
catch (Exception)
{
Response.StatusCode = 204;
}
}
}
Dimension
Set the default width, minWidth, height and minHeight of the video element, when it is inserted in the Rich Text Editor using the Width, MinWidth, Height, MinHeight properties.
Through the QuickToolbarSettings, also you can change the width and height using Change Size
option. Once you click on the option, the video size dialog will open as below. In that, specify the width and height of the video in pixels
Display Position
Sets the default display property for the video when it is inserted in the Rich Text Editor using the InsertVideoSettings.LayoutOption
. It has two possible options: Inline
and Break
. When updating the display positions, it updates the video elements’ layout position.
NOTE
The default
LayoutOption
property is set toInline
.
Sets the default display for an video when it is inserted in the Rich Text Editor using the InsertVideoSettings.LayoutOption
. It has two possible options: Inline
and Break
. When updating the display positions, it updates the video elements’ layout position.
@Html.EJS().RichTextEditor("video").ToolbarSettings(e => e.Items((object)ViewBag.items)).ContentTemplate(@<div>
<p>
The Rich Text Editor control is WYSIWYG ('what you see is what you get') editor that provides the best user experience to create and update the content.
Users can format their content using standard toolbar commands.
</p>
<p><b> Key features:</b></p>
<ul>
<li><p> Provides < IFRAME > and < DIV > modes </p></li>
<li><p> Capable of handling markdown editing.</p></li>
<li><p> Contains a modular library to load the necessary functionality on demand.</p></li>
<li><p> Provides a fully customizable toolbar.</p></li>
<li><p> Provides HTML view to edit the source directly for developers.</p></li>
<li><p> Supports third - party library integration.</p></li>
<li><p> Allows preview of modified content before saving it.</p></li>
<li><p> Handles images, hyperlinks, video, hyperlinks, uploads, etc.</p></li>
<li><p> Contains undo / redo manager.</p></li>
<li><p> Creates bulleted and numbered lists.</p></li>
</ul>
</div>).InsertVideoSettings(obj => obj.LayoutOption("Break")).Render()
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.items = new[] { "Video" };
return View();
}
}
Resize video
The Rich Text Editor has built-in video resizing support, which is enabled for the video elements added. The resize points will appear on each corner of the video when focusing so users can easily resize the video using mouse points or thumb through the resize points. Also, the resize calculation will be done based on the aspect ratio.
You can disable the resize action by configuring false
for the InsertVideoSettings.Resize property.
NOTE
If the MinWidth and MinHeight properties are configured the video resizing does not shrink below the specified values.
Rename video before inserting
By using the InsertVideoSettings property, you can specify the server handler to upload the selected video. Then by binding the FileUploadSuccess
event, you can receive the modified file name from the server and update it in the Rich Text Editor’s insert video dialog.
Refer rename.cs
controller file for configure the server-side.
@Html.EJS().RichTextEditor("editor").ToolbarSettings(e => e.Items((object)ViewBag.items)).InsertVideoSettings(obj => obj.SaveUrl("/Home/Rename").Path("../Uploads/")).FileUploadSuccess("onFileUploadSuccess").Render()
<script>
function onFileUploadSuccess(args) {
if (args.e.currentTarget.getResponseHeader('name') != null) {
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>
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace RenameFile.Controllers
{
public class HomeController : Controller
{
int x = 0;
string filename;
public ActionResult Index()
{
ViewBag.items = new[] { "Video" };
return View();
}
[AcceptVerbs("Post")]
public void Rename(HttpPostedFileBase UploadFiles)
{
try
{
if (UploadFiles != null)
{
filename = UploadFiles.FileName;
if (UploadFiles != null)
{
var fileSave = System.Web.HttpContext.Current.Server.MapPath("~/Uploads");
// Create a new directory, if it does not exists
if (!System.IO.Directory.Exists(fileSave))
{
System.IO.Directory.CreateDirectory(fileSave);
}
var fileName = Path.GetFileName(UploadFiles.FileName);
var fileSavePath = Path.Combine(fileSave, fileName);
// Rename a uploaded file name
while (System.IO.File.Exists(fileSavePath))
{
filename = "rteVideo" + x + "-" + fileName;
fileSavePath = Path.Combine(fileSave, filename);
x++;
}
if (!System.IO.File.Exists(fileSavePath))
{
UploadFiles.SaveAs(fileSavePath);
// Modified file name shared through response header by adding custom header
Response.Headers.Add("name", filename);
Response.StatusCode = 200;
Response.ContentType = "application/json; charset=utf-8";
}
}
}
}
catch (Exception)
{
Response.StatusCode = 204;
}
}
}
}
Upload video with authentication
The Rich Text Editor control allows you to add additional data with the File Upload, which can be received on the server side. By using the FileUploading
event and its customFormData
argument, you can pass parameters to the controller action. On the server side, you can fetch the custom headers by accessing the form collection from the current request, which retrieves the values sent using the POST method.
NOTE
By default, it doesn’t support the
UseDefaultCredentials
property, you can manually append the default credentials with the upload request.
By default it doesn’t support
UseDefaultCredentials
property, we need to manually append the default credentials with the upload request.
@Html.EJS().RichTextEditor("defaultRTE").ToolbarSettings(e => e.Items((object)ViewBag.items)).InsertVideoSettings(obj => obj.SaveUrl("/Home/SaveFiles").FileUploading("onFileUploading").Path("../Files/")).Render()
<script>
function onFileUploading(args) {
var accessToken = "Authorization_token";
// adding custom Form Data
args.customFormData = [{ 'Authorization': accessToken }];
}
</script>
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace FileUpload.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.items = new[] { "Video" };
return View();
}
[AcceptVerbs("Post")]
public void SaveFiles(HttpPostedFileBase UploadFiles)
{
string currentPath = Request.Form["Authorization"].ToString();
if (UploadFiles != null)
{
string path = Server.MapPath("~/Files/");
// Create a new directory, if it does not exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
// Save a file in directory
UploadFiles.SaveAs(path + Path.GetFileName(UploadFiles.FileName));
}
}
}
}