Audios in React Rich Text Editor Component
4 Sep 202524 minutes to read
The Rich Text Editor enables insertion of audio files from online sources or local machines. You can insert the audio with the following list of options in the insertAudioSettings property.
Configuring the audio toolbar item
The audio feature is enabled by adding the Audio item to the toolbar using the toolbarSettings.items property. The AudioService must be injected as shown below:
To use Audio feature, inject audio module using the
<Inject services={[Audio]} />.
The following example demonstrates configuring the audio toolbar item:
[Class-component]
import * as React from 'react';
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
class App extends React.Component {
toolbarSettings = {
items: ['Audio']
};
rteValue= " <p>Rich Text Editor allows inserting video and audio from online sources and the local computers where you want to insert a video and audio into your content.</p>";
insertAudioSettings = {
saveUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Save',
removeUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Remove'
};
render() {
return (<RichTextEditorComponent height={450} value={this.rteValue} toolbarSettings={this.toolbarSettings} insertAudioSettings={this.insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]}/>
</RichTextEditorComponent>);
}
}
export default App;import * as React from 'react';
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
class App extends React.Component<{},{}> {
private toolbarSettings: object = {
items: ['Audio']
}
private rteValue: string= " <p>Rich Text Editor allows inserting video and audio from online sources and the local computers where you want to insert a video and audio into your content.</p>";
private insertAudioSettings: object = {
saveUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Save',
removeUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Remove'
}
public render() {
return (
<RichTextEditorComponent height={450} value={this.rteValue} toolbarSettings={this.toolbarSettings} insertAudioSettings = {this.insertAudioSettings} >
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
}
export default App;[Functional-component]
import * as React from 'react';
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
function App() {
let toolbarSettings = {
items: ['Audio']
};
let rteValue= " <p>Rich Text Editor allows inserting video and audio from online sources and the local computers where you want to insert a video and audio into your content.</p>";
let insertAudioSettings = {
saveUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Save',
removeUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Remove'
};
return (<RichTextEditorComponent value={rteValue} height={450} toolbarSettings={toolbarSettings} insertAudioSettings={insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]}/>
</RichTextEditorComponent>);
}
export default App;import * as React from 'react';
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
function App() {
let toolbarSettings: object = {
items: ['Audio']
}
let rteValue: string = " <p>Rich Text Editor allows inserting video and audio from online sources and the local computers where you want to insert a video and audio into your content.</p>";
let insertAudioSettings: object = {
saveUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Save',
removeUrl: 'https://ej2.syncfusion.com/services/api/uploadbox/Remove'
}
return (
<RichTextEditorComponent height={450} value={rteValue} toolbarSettings={toolbarSettings} insertAudioSettings = {insertAudioSettings} >
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
export default App;Audio save formats
The audio files can be saved as Blob or Base64 URL by using the insertAudioSettings.saveFormat property, which is of enum type, and the generated URL will be set to the src attribute of the <source> tag.
By default, the
saveFormatis set toBlob.
<audio>
<source src="blob:http://ej2.syncfusion.com/3ab56a6e-ec0d-490f-85a5-f0aeb0ad8879" type="audio/mp3" >
</audio>
<audio>
<source src="data:audio/mp3;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHA" type="audio/mp3" >
</audio>Inserting audio
You can insert audio from either the hosted link or the local machine, by clicking the audio button in the editor’s toolbar. On clicking the audio button, a dialog opens, which allows you to insert audio from the web URL.
Inserting audio from web URLs
By default, the audio toolbar item opens a dialog for inserting audio from an online source. Entering a valid URL will be added to the src attribute of the <source> tag.

Uploading audio from local machine
The audio dialog includes a browse option to select audio file from a local machine and insert it into the Rich Text Editor content.
If the insertAudioSettings.path is not specified, the audio is converted to a Blob or Base64 URL and inserted into the editor.
Maximum file size restriction
You can restrict the audio uploaded from the local machine when the uploaded audio file size is greater than the allowed size by using the insertAudioSettings.maxFileSize property. By default, the maximum file size is 30000000 bytes. You can configure this size as follows.
In the following illustration, the audio size has been validated before uploading, and it is determined whether the audio has been uploaded or not.
[Class-component]
import { HtmlEditor, Image, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar, Table, Audio, PasteCleanup } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
class App extends React.Component<{},{}> {
private toolbarSettings: object = {
items: ['Audio', 'Bold', 'Italic', 'Underline', '|', 'Formats', 'Alignments', 'Blockquote', 'OrderedList', 'UnorderedList', '|', 'CreateLink', 'CreateTable', 'Image', '|', 'SourceCode', '|', 'Undo', 'Redo']
}
private insertAudioSettings: object = {
maxFileSize: 30000000
}
public render() {
return (
<RichTextEditorComponent height={450} toolbarSettings={this.toolbarSettings} insertAudioSettings={
this.insertAudioSettings}>
<Inject services={[HtmlEditor, Toolbar, Image, Link, QuickToolbar, Table, Audio, PasteCleanup]} />
</RichTextEditorComponent>
);
}
}
export default App;[Functional-component]
import { HtmlEditor, Image, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar, Table, Audio, PasteCleanup } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
function App() {
let toolbarSettings: object = {
items: ['Audio', 'Bold', 'Italic', 'Underline', '|', 'Formats', 'Alignments', 'Blockquote', 'OrderedList', 'UnorderedList', '|', 'CreateLink', 'CreateTable', 'Image', '|', 'SourceCode', '|', 'Undo', 'Redo']
}
let insertAudioSettings: object = {
maxFileSize: 30000000
}
return (
<RichTextEditorComponent height={450} toolbarSettings={toolbarSettings} insertAudioSettings={insertAudioSettings}>
<Inject services={[HtmlEditor, Toolbar, Image, Link, QuickToolbar, Table, Audio, PasteCleanup]} />
</RichTextEditorComponent>
);
}
export default App;Saving audio to the server
saveFormat sets the default save format of the audio element when inserted. Possible options are: Blob and Base64.
saveUrl provides URL to map the action result method to save the audio.
removeUrl provides URL to map the action result method to remove the audio.
Server-side action
The selected audio can be uploaded to the required destination using the controller action below. Map this method name in insertAudioSettings.saveUrl and provide the required destination path through insertAudioSettings.path properties.
If you want to insert lower-sized audio files in the editor and don’t want a specific physical location for saving the audio, you can opt to save the format as
Base64.
In the following code blocks, the audio module has been injected and can insert the audio files saved in the specified path.
[Class-component]
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
class App extends React.Component<{},{}> {
private toolbarSettings: object = {
items: ['Audio']
}
private insertAudioSettings: object = {
saveUrl: "[SERVICE_HOSTED_PATH]/api/uploadbox/SaveFiles",
path: "[SERVICE_HOSTED_PATH]/Files/"
}
public render() {
return (
<RichTextEditorComponent height={450} toolbarSettings={this.toolbarSettings} insertAudioSettings={this.insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
}
export default App;[Functional-component]
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
function App(){
let toolbarSettings: object = {
items: ['Audio']
}
let insertAudioSettings: object = {
saveUrl: "[SERVICE_HOSTED_PATH]/api/uploadbox/SaveFiles",
path: "[SERVICE_HOSTED_PATH]/Files/"
}
return (
<RichTextEditorComponent height={450} toolbarSettings={toolbarSettings} insertAudioSettings={insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
export default App;using System;
using System.IO;
using FileUpload.Models;
using System.Diagnostics;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
namespace FileUpload.Controllers
{
public class HomeController : Controller
{
private IHostingEnvironment hostingEnv;
public HomeController(IHostingEnvironment env)
{
hostingEnv = env;
}
public IActionResult Index()
{
return View();
}
[AcceptVerbs("Post")]
public void SaveFiles(IList<IFormFile> UploadFiles)
{
try
{
foreach (IFormFile file in UploadFiles)
{
if (UploadFiles != null)
{
string filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
filename = hostingEnv.WebRootPath + "\\Files" + $@"\{filename}";
// Create a new directory, if it does not exists
if (!Directory.Exists(hostingEnv.WebRootPath + "\\Files"))
{
Directory.CreateDirectory(hostingEnv.WebRootPath + "\\Files");
}
if (!System.IO.File.Exists(filename))
{
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
Response.StatusCode = 200;
}
}
}
}
catch (Exception)
{
Response.StatusCode = 204;
}
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}Renaming audio before inserting
You can use the insertAudioSettings property, to specify the server handler to upload the selected audio. Then by binding the fileUploadSuccess event allows renaming audio files before insertion by updating the file name in the audio dialog:
[Class-component]
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
class App extends React.Component<{},{}> {
private toolbarSettings: object = {
items: ['Audio']
}
private insertAudioSettings: object = {
saveUrl: "[SERVICE_HOSTED_PATH]/api/uploadbox/Rename",
path: "[SERVICE_HOSTED_PATH]/Files/"
}
private onFileUploadSuccess (args: any) {
alert("Get the new file name here");
if (args.e.currentTarget.getResponseHeader('name') != null) {
args.file.name = args.e.currentTarget.getResponseHeader('name');
let filename: any = document.querySelectorAll(".e-file-name")[0];
filename.innerHTML = args.file.name.replace(document.querySelectorAll(".e-file-type")[0].innerHTML, '');
filename.title = args.file.name;
}
}
public render() {
return (
<RichTextEditorComponent height={450} toolbarSettings={this.toolbarSettings} fileUploadSuccess={this.onFileUploadSuccess} insertAudioSettings={this.insertAudioSettings} >
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
<p>The Rich Text Editor is WYSIWYG ("what you see is what you get") editor useful to create and edit content, and return the valid <a href="https://ej2.syncfusion.com/home/" target="_blank">HTML markup</a> or <a href="https://ej2.syncfusion.com/home/" target="_blank">markdown</a> of the content</p>
</RichTextEditorComponent>
);
}
}
export default App;[Functional-component]
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
function App (){
let toolbarSettings: object = {
items: ['Audio']
}
let insertAudioSettings: object = {
saveUrl: "[SERVICE_HOSTED_PATH]/api/uploadbox/Rename",
path: "[SERVICE_HOSTED_PATH]/Files/"
}
function onFileUploadSuccess (args: any) {
alert("Get the new file name here");
if (args.e.currentTarget.getResponseHeader('name') != null) {
args.file.name = args.e.currentTarget.getResponseHeader('name');
let filename: any = document.querySelectorAll(".e-file-name")[0];
filename.innerHTML = args.file.name.replace(document.querySelectorAll(".e-file-type")[0].innerHTML, '');
filename.title = args.file.name;
}
}
return (
<RichTextEditorComponent height={450} toolbarSettings={toolbarSettings} fileUploadSuccess={this.onFileUploadSuccess} insertAudioSettings={insertAudioSettings} >
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
<p>The Rich Text Editor is WYSIWYG ("what you see is what you get") editor useful to create and edit content, and return the valid <a href="https://ej2.syncfusion.com/home/" target="_blank">HTML markup</a> or <a href="https://ej2.syncfusion.com/home/" target="_blank">markdown</a> of the content</p>
</RichTextEditorComponent>
);
}
export default App;To configure server-side handler, refer to the below code.
int x = 0;
string file;
[AcceptVerbs("Post")]
public void Rename()
{
try
{
var httpPostedFile = System.Web.HttpContext.Current.Request.Files["UploadFiles"];
fileName = httpPostedFile.FileName;
if (httpPostedFile != null)
{
var fileSave = System.Web.HttpContext.Current.Server.MapPath("~/Files");
if (!Directory.Exists(fileSave))
{
Directory.CreateDirectory(fileSave);
}
var fileName = Path.GetFileName(httpPostedFile.FileName);
var fileSavePath = Path.Combine(fileSave, fileName);
while (System.IO.File.Exists(fileSavePath))
{
fileName = "rteFiles" + x + "-" + fileName;
fileSavePath = Path.Combine(fileSave, fileName);
x++;
}
if (!System.IO.File.Exists(fileSavePath))
{
httpPostedFile.SaveAs(fileSavePath);
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.Headers.Add("name", fileName);
Response.ContentType = "application/json; charset=utf-8";
Response.StatusDescription = "File uploaded succesfully";
Response.End();
}
}
}
catch (Exception e)
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = 204;
Response.Status = "204 No Content";
Response.StatusDescription = e.Message;
Response.End();
}
}Uploading audio with authentication
You can add additional data with the audio uploaded from the Rich Text Editor on the client side, which can even 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.
By default, it doesn’t support the
UseDefaultCredentialsproperty; we need to manually append the default credentials with the upload request.
[Class-component]
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
import { UploadingEventArgs } from '@syncfusion/ej2-inputs';
class App extends React.Component<{},{}> {
private toolbarSettings: object = {
items: ['Audio']
}
private insertAudioSettings: object = {
saveUrl: "[SERVICE_HOSTED_PATH]/api/uploadbox/SaveFiles",
path: "[SERVICE_HOSTED_PATH]/Files/"
}
private onFileUpload (args: UploadingEventArgs): void {
var accessToken = "Authorization_token";
// adding custom Form Data
args.customFormData = [{ 'Authorization': accessToken }];
}
public render() {
return (
<RichTextEditorComponent height={450} toolbarSettings={this.toolbarSettings} insertAudioSettings={
this.insertAudioSettings} fileUploading = {this.onFileUpload}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
}[Functional-component]
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
import { UploadingEventArgs } from '@syncfusion/ej2-inputs';
function App() {
let toolbarSettings: object = {
items: ['Audio']
}
let insertAudioSettings: object = {
saveUrl: "[SERVICE_HOSTED_PATH]/api/uploadbox/SaveFiles",
path: "[SERVICE_HOSTED_PATH]/Files/"
}
function onFileUpload (args: UploadingEventArgs): void {
var accessToken = "Authorization_token";
// adding custom Form Data
args.customFormData = [{ 'Authorization': accessToken }];
}
return (
<RichTextEditorComponent height={450} toolbarSettings={toolbarSettings} insertAudioSettings={insertAudioSettings} fileUploading = {onFileUpload}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}public void SaveFiles(IList<IFormFile> UploadFiles)
{
string currentPath = Request.Form["Authorization"].ToString();
}Audio replacement functionality
The quickToolbarSettings.audioReplace audioReplace option. You can replace the audio file using the web URL or the browse option in the audio dialog.

Deleting audios
To delete an audio file, select it and click the audioRemove button in the quick toolbar. It will delete the audio from the Rich Text Editor content as well as from the service location if the insertAudioSettings.removeUrl is given.
When an audio file is selected from the local machine, a URL is generated for it. You can remove the audio from the service location by clicking the cross icon in the audio dialog.

Configuring audio display position
Sets the default display property for audio when it is inserted in the Rich Text Editor using the insertAudioSettings.layoutOption property. It has two possible options: Inline and Break. When updating the display positions, it updates the audio elements layout position.
The default
layoutOptionproperty is set toInline.
[Class-component]
import * as React from 'react';
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
class App extends React.Component<{},{}> {
private insertAudioSettings: object = {
layoutOption: 'Inline',
}
public render() {
return (
<RichTextEditorComponent height={450} insertAudioSettings = {this.insertAudioSettings} >
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
}[Functional-component]
import * as React from 'react';
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
function App() {
let insertAudioSettings: object = {
layoutOption: 'Inline',
}
return (
<RichTextEditorComponent height={450} insertAudioSettings = {insertAudioSettings} >
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}Drag and drop audio insertion
By default, the Rich Text Editor allows you to insert audios by drag-and-drop from the local file system such as Windows Explorer into the content editor area. And, you can upload the audios to the server before inserting into the editor by configuring the saveUrl property.
In the following sample, you can see feature demo.
[Class-component]
/**
* Initilaize Rich Text Editor from React element
*/
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
class App extends React.Component {
insertAudioSettings = {
saveUrl: 'https://services.syncfusion.com/react/production/api/RichTextEditor/SaveFile'
};
render() {
return (<RichTextEditorComponent insertAudioSettings={this.insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]}/>
</RichTextEditorComponent>);
}
}
export default App;/**
* Initilaize Rich Text Editor from React element
*/
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
class App extends React.Component<{},{}> {
public insertAudioSettings: object = {
saveUrl: 'https://services.syncfusion.com/react/production/api/RichTextEditor/SaveFile'
};
public render() {
return (
<RichTextEditorComponent insertAudioSettings={this.insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
}
export default App;[Functional-component]
/**
* Initilaize Rich Text Editor from React element
*/
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
function App() {
let insertAudioSettings = {
saveUrl: 'https://services.syncfusion.com/react/production/api/RichTextEditor/SaveFile'
};
return (<RichTextEditorComponent insertAudioSettings={insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]}/>
</RichTextEditorComponent>);
}
export default App;/**
* Initilaize Rich Text Editor from React element
*/
import { HtmlEditor, Audio, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';
function App() {
let insertAudioSettings: object = {
saveUrl: 'https://services.syncfusion.com/react/production/api/RichTextEditor/SaveFile'
};
return (
<RichTextEditorComponent insertAudioSettings={insertAudioSettings}>
<Inject services={[Toolbar, Audio, Link, HtmlEditor, QuickToolbar]} />
</RichTextEditorComponent>
);
}
export default App;Disabling audio drag and drop
You can prevent drag-and-drop action by setting the actionBegin argument cancel value to true. The following code shows how to prevent the drag-and-drop.
actionBegin: function (args: any): void {
if(args.type === 'drop' || args.type === 'dragstart') {
args.cancel =true;
}
}