Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert file into base64 in javascript

function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}

var file = document.querySelector('#files > input[type="file"]').files[0];
getBase64(file).then(
  data => console.log(data)
);
Comment

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);});
Comment

PREVIOUS NEXT
Code Example
Javascript :: Pass Props to a Component Using defaultProps in react 
Javascript :: select element by id 
Javascript :: using / for division is deprecated and will be removed in dart sass 2.0.0 
Javascript :: remove object in array javascript 
Javascript :: react prevent form submission on enter key press inside inputs 
Javascript :: while and do while loop in javascript 
Javascript :: string number to array 
Javascript :: javascript detect tab leave 
Javascript :: what is div in javascript 
Javascript :: javascript split regex new line 
Javascript :: java parse json 
Javascript :: jquery show hide based on data attribute 
Javascript :: how to use saved image on react 
Javascript :: sum all values of an array 
Javascript :: check if number appears odd number of times in array javascript 
Javascript :: stringify json javascript 
Javascript :: javascript show page 
Javascript :: javascript insert html before element 
Javascript :: how get height elemnt with that margin in js 
Javascript :: looping queryselectorall 
Javascript :: add two numbers in javascript 
Javascript :: unidirectional data flow 
Javascript :: ejs include with variable 
Javascript :: add image to ag-grid with react 
Javascript :: axios check 401 run function 
Javascript :: image in react jsx 
Javascript :: eslint disable tag 
Javascript :: Integrating Axios with React Hooks 
Javascript :: react-native-infinite-scroll-with-flatlist 
Javascript :: code that will execute at a certain day and time javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =