Articles in this section
Category / Section

how to upload /add image in symbol palette in JavaScript

3 mins read

This article provides an explanation of how to upload and add an image to the symbol palette using the uploader component in the Syncfusion® JavaScript Diagram Component. In the provided example, we demonstrate how to add a button to facilitate importing an image from the local system. When the button is clicked, it triggers a file selection dialog that allows users to choose an image from their local storage.

   button.element.onclick = function () {
       // Trigger the click event of the hidden file input element
       document.getElementsByClassName('e-file-select-wrap')[0].querySelector('button').click();
       return false;
   }; 

Once an image is selected, the application processes the upload, and upon successful completion, the onUploadSuccess method is invoked. This method is responsible for updating the symbol palette by adding the newly uploaded image as a node.

function onUploadSuccess(arg) {
       // Retrieve the uploaded file
       var file1 = arg.file;
       var file = file1.rawFile;
       // Create a new FileReader instance to read the file content
       var reader = new FileReader();
       // Initialize variables to store shape details
       reader.addEventListener('load', function (event) {
           var shape;
           var shapeContent = event.target.result;
           shape = { id: 'newshape' + id.toString(), shape: { type: 'Image', source: shapeContent } };
           palette.addPaletteItem('network', shape);
       });
       id++;
       reader.readAsDataURL(file);
       // Clear the uploader to prepare for the next upload
       uploadObj.clearAll();
   } 

After the image is added to the symbol palette, it becomes available for use. You can drag the image from the palette and drop it onto the diagram canvas, seamlessly integrating it into your workflow. This feature enhances the user experience by allowing dynamic updates to the palette and enabling users to personalize their diagrams with custom images.

Please refer to the working sample for reference.
Sample

Conclusion

I hope you enjoyed learning how to upload /add image in symbol palette of JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Diagram example to understand how to create and manipulate data. For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied