In this article, we will learn how can we allow users to upload images in a rich text editor in the SPFx web part.
When you copy and paste the image in React quill text editor, it will be pasted successfully and if you inspect this image from the developer tool of the browser you can see there will be a base64 string of images.
And when you save it, it will not be saved in the rich text column of your SharePoint list. Even only copy-paste in a rich text column in the list directly will also not save the image. SharePoint column will store the image in a rich text editor if the reference is of any SharePoint itself or any image URL which is accessible anonymously.
So, let’s see how can we achieve image upload functionality in the SPFx web part’s rich text editor control.
Follow the below steps to achieve this functionality:


Step 1
Upload package of the react quill using the below command:
Step 2
Import package in SPFx web part as below:
Step 3
Declare a global variable. This variable will be used as context for our rich text editor control.
Step 4
Add react quill control in render method as below:
  • In the above code, you can see [‘link’, ‘image’] in the container options. When you add an Image in the container array, it will display the image upload icon in the rich text control as shown in the below screenshot.
Step 5
Now when clicks on the Image upload icon, we need to define which method should be called. So we need to define this method in handlers. So in control, we need to define handlers in the toolbar section:
Step 6
Now imageHandler method will have code as below,
  • When the user clicks on the image icon of rich text control, it will call the above method. This method will create a control for file upload and call on the click method of file upload control.
  • So, on clicking the image icon of the rich text box, it will open the file selection popup and allow to select only the image file.
  • In imageHandler method, we also defined what should be done once the user selects the image from the file selection popup.
Step 7
Now, when the user selects the image, we need to upload the selected image in the particular SharePoint document library. So, create new document library if you want to upload an image in the specific document library, else you can also use the default available Shared Documents library.
Step 8
Here in the above code also you can see we are calling uploadFiles method. The code in the uploadfiles method will be as below,
In the above code, the document library name is ImageFiles. Once the file is uploaded successfully, we will return the URL of the uploaded image to a rich text editor and will render the image in rich text control using the below line of code
  1. quillObj.getEditor().insertEmbed(range.index, ‘image’, res);
  • The image will be rendered at the particular place where the cursor’s position using quillObj which we have defined as a global variable and then used while rendering control.
  • Now when the user selects the image, it will first be uploaded to the SharePoint document library and then use the uploaded files URL to load the image in the rich text box.
  • When you inspect the image in rich text control, you can see the URL of the image, not the base 64 string of the image.
  • While saving into the list, when you get the value of rich text control, it will return the image control with the URL of the image. So in the list also it will store the URL of an image.
This is how we can use image upload functionality in the react quill rich text editor in the SPFx custom web part.