Where I Can Upload My Finished Quill Imaje

  • Updated date Mar xvi, 2021
  • 29.7k
  • 3

In this commodity, we will acquire how tin can nosotros allow users to upload images in a rich text editor in the SPFx web part.

Introduction

In this article, we will larn how can we allow users to upload images in a rich text editor in the SPFx web part.

When y'all copy and paste the image in React quill text editor, it volition exist pasted successfully and if yous audit this epitome from the developer tool of the browser you lot can see there will be a base64 string of images.

And when y'all save it, it will not be saved in the rich text column of your SharePoint list. Fifty-fifty only re-create-paste in a rich text column in the list directly volition too 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 role's rich text editor command.

Follow the below steps to achieve this functionality:

Steps

Step one

Upload parcel of the react quill using the below control:

  1. npm i react-quill

Step two

Import package in SPFx web part as below:

  1. import ReactQuill from 'react-quill' ;

Step 3

Declare a global variable. This variable will be used every bit context for our rich text editor control.

  1. var quillObj: any;

Step 4

Add together react quill control in render method equally below:

  1. <ReactQuill
  2. ref ={(el) => {
  3.                 quillObj = el;
  4.               }}
  5.               value={this .state.Clarification}
  6.               modules={{
  7.                 toolbar: {
  8.                   container: [
  9.                     [{'header' : [1, 2, 3, 4, 5, 6, false ] }],
  10.                     ['bold' , 'italic' , 'underline' ],
  11.                     [{'listing' : 'ordered'  }, { 'list' : 'bullet'  }],
  12.                     [{'align' : [] }],
  13.                     ['link' , 'epitome' ],
  14.                     ['clean' ],
  15.                     [{'color' : [] }]
  16.                   ],
  17.                   handlers: {
  18.                     epitome:this .imageHandler
  19.                   }
  20.                 },
  21.                 table:true
  22.               }}
  23.               placeholder="Add a clarification of your event"
  24.               onChange={(content, delta, source, editor) =>this .onDescriptionChange(content, editor)}
  25.               id="txtDescription"
  26.             />
  • In the to a higher place code, you tin see ['link', 'paradigm'] in the container options. When yous add an Image in the container array, it volition display the image upload icon in the rich text command as shown in the beneath screenshot.

Step 5

At present when clicks on the Image upload icon, we need to define which method should be called. And then we need to define this method in handlers. And so in command, we need to ascertain handlers in the toolbar section:

  1. handlers: {
  2.    image:this .imageHandler
  3. }

Step half-dozen

Now imageHandler method will have code as below,

  1. public  async imageHandler() {
  2. const  input = document.createElement( 'input' );
  3.     input.setAttribute('type' , 'file' );
  4.     input.setAttribute('accept' , 'image/*' );
  5.     input.click();
  6.     input.onchange = async () => {
  7.       var file: any = input.files[0];
  8.       var formData =new  FormData();
  9.       formData.append('paradigm' , file);
  10.       var fileName = file.proper name;
  11. const  res = await this .uploadFiles(file, fileName, quillObj);
  12.     };
  13.   }
  • When the user clicks on the image icon of rich text control, it will phone call the above method. This method will create a command for file upload and call on the click method of file upload control.
  • So, on clicking the paradigm icon of the rich text box, it will open up the file selection popup and let to select but the image file.
  • In imageHandler method, we as well defined what should be done in one case the user selects the image from the file selection popup.

Step 7

Now, when the user selects the epitome, we need to upload the selected image in the item SharePoint document library. So, create new document library if you want to upload an image in the specific document library, else yous can also employ the default bachelor Shared Documents library.

Stride viii

Here in the above code also you can meet nosotros are calling uploadFiles method. The code in the uploadfiles method volition be equally below,

  1. public  async uploadFiles(uploadFileObj, filename, quillObj) {
  2.     var libraryName ="ImageFiles" ;
  3.     var context =this .props.context;
  4.     var siteUrl =this .props.context.pageContext.site.absoluteUrl;
  5.     var currentdate =new  Date();
  6.     var fileNamePredecessor = currentdate.getDate().toString() + currentdate.getMonth().toString() + currentdate.getFullYear().toString() + currentdate.getTime().toString();
  7.     filename = fileNamePredecessor + filename;
  8.     var apiUrl = `${siteUrl}/_api/Spider web/Lists/getByTitle('${libraryName}' )/RootFolder/Files/Add(url= '${filename}' , overwrite= truthful )`;
  9. const  digestCache: IDigestCache = this .props.context.serviceScope.eat(DigestCache.serviceKey);
  10.     digestCache.fetchDigest(
  11. this .props.context.pageContext.web.serverRelativeUrl)
  12.       .then(async (assimilate:string ): Promise< void > => {
  13. effort  {
  14. if  (uploadFileObj != '' ) {
  15.             fetch(apiUrl, {
  16.               method:'Postal service' ,
  17.               headers: {
  18. 'Content-Type' : 'awarding/json;odata=verbose' ,
  19. "Ten-RequestDigest" : assimilate
  20.               },
  21.               body: uploadFileObj
  22.             }).then((response) => {
  23.               panel.log(response);
  24. const  range = quillObj.getEditorSelection();
  25.               var res = siteUrl +"/"  + listName + "/"  + filename;
  26.               quillObj.getEditor().insertEmbed(range.index,'image' , res);
  27.             }).grab ((error) =>
  28.               panel.log(error)
  29.             );
  30.           }
  31.         }
  32. catch  (error) {
  33.           panel.log('uploadFiles : '  + error);
  34.         }
  35.       })
  36.   }

In the in a higher place code, the document library name is ImageFiles. One time the file is uploaded successfully, we will render the URL of the uploaded paradigm to a rich text editor and will render the image in rich text control using the beneath line of code

  1. quillObj.getEditor().insertEmbed(range.index, 'prototype' , res);
  • The epitome will be rendered at the detail place where the cursor'southward position using quillObj which we have defined as a global variable and and then used while rendering command.
  • Now when the user selects the paradigm, it will first be uploaded to the SharePoint certificate library and then apply the uploaded files URL to load the prototype in the rich text box.

  • When you inspect the image in rich text control, you lot can see the URL of the image, non the base 64 string of the image.

  • While saving into the listing, when you get the value of rich text control, it will render the paradigm control with the URL of the image. So in the list also information technology will store the URL of an image.

Conclusion

This is how we tin use image upload functionality in the react quill rich text editor in the SPFx custom web part.

Promise this article will be helpful!

herringherl1981.blogspot.com

Source: https://www.c-sharpcorner.com/article/how-to-add-image-upload-control-in-react-quill-rich-text-editor/

0 Response to "Where I Can Upload My Finished Quill Imaje"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel