
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Image to Base64 String Using JavaScript
To convert the image into a base64 string using JavaScript, use the FileReader API. You can try to run the following code to get base64string for an image −
Example
<!DOCTYPE html> <html> <body> <script> function toDataURL(url, callback) { var httpRequest = new XMLHttpRequest(); httpRequest.onload = function() { var fileReader = new FileReader(); fileReader.onloadend = function() { callback(fileReader.result); } fileReader.readAsDataURL(httpRequest.response); }; httpRequest.open('GET', url); httpRequest.responseType = 'blob'; httpRequest.send(); } toDataURL('https://www.tutorialspoint.com/videotutorials/images/tutor_connect_home.jpg', function(dataUrl) { document.write('Result in string:', dataUrl) }) </script> </body> </html>
Advertisements