Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Converting file to base64 on Javascript client side

// Convert file to base64 string
export const fileToBase64 = (filename, filepath) => { 
return new Promise(resolve => {    
var file = new File([filename], filepath);    
  var reader = new FileReader();    // Read file content on file loaded event
  reader.onload = function(event) { 
    resolve(event.target.result);    };        // Convert data to base64
  reader.readAsDataURL(file);  
});
};
// Example call:
fileToBase64("test.pdf", "../files/test.pdf").then(result => {  console.log(result);});
Source by medium.com #
 
PREVIOUS NEXT
Tagged: #Converting #file #Javascript #client #side
ADD COMMENT
Topic
Name
1+4 =