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

javascript base64 encode file input

var fileInput = document.getElementById('#myInputID');

var reader = new FileReader();
reader.readAsDataURL(fileInput.files[0]);

reader.onload = function () {
	console.log(reader.result);//base64encoded string
};
reader.onerror = function (error) {
	console.log('Error: ', error);
};
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 :: remove first and last character from string javascript 
Javascript :: extract filename from content-disposition header js 
Javascript :: JS get number of classes in html 
Javascript :: javascript size of variable in kb 
Javascript :: rounded TouchableNativeFeedback 
Javascript :: creating a custom router class in backbone 
Javascript :: if button is keeping pressed execute increment javascript 
Javascript :: how to delete all slash command discord.js 
Javascript :: checking if var is not defined js 
Javascript :: update node ubuntu 
Javascript :: keyboard dismiss react native 
Javascript :: how to add event listener to iframe 
Javascript :: set background image in material ui 
Javascript :: check if object is empty javascript 
Javascript :: how to convert time to am pm in javascript 
Javascript :: $.dialog after create focus 
Javascript :: how to access variables from render() to outside of render() in class react component 
Javascript :: javascript new date zero time 
Javascript :: dummy json 
Javascript :: moment js date diff 
Javascript :: add option to select jquery 
Javascript :: mouseevent clientx javascript 
Javascript :: minecraft infinite snapshot dimensions 
Javascript :: next.js how to add google fonts 
Javascript :: js add params to url 
Javascript :: Could not resolve dependency: npm ERR! peer reac 
Javascript :: gmail regex 
Javascript :: disable right click html w3schools 
Javascript :: axios jwt 
Javascript :: javascript json upload 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =