I am using angular 1.7 with material design for my project. i am using angular material file input for this but it gives me result in file format. what i need is to convert that file format into base64 format. i have tried some external libraries but it doesn't work for me. can anyone post code or make a codepen/jsfiddle.
You can try this solution (this is o copy of this post: How to convert file to base64 in JavaScript? ):
function getBase64(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
var file = /*Your file goes here*/
getBase64(file); // prints the base64 string
Related
I used cordova for my application, I want to read all type of file format (ex:image,xls,pdf and etc), for uploading but filereader's working only for image format not for other formats,
Do we have any option to allows formats
e.target.result - will come with null value from filreader
sample code:
let reader = new FileReader();
entry.file(function (file) {
reader.onloadend = function (e) {
var content = e.target.result; // null value when choose .xml,pdf
console.log(content)
};
reader.readAsDataURL(file);
});
my project have this foldes:
-html
index.html
-css
-js
my.js
-pdf
test.pdf
I need to convert this "test.pdf" to base 64 and send by POST
I try use a function like this:
function getBase64() {
var reader = new FileReader();
var file = new File("/pdf/test.pdf","r");
reader.addEventListener("loadend", function() {
// reader.result contains the contents of blob as a typed array
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
return reader.result;
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
});
But I'm not the correct way to get past the folder path, or if I have to use another object and not File.
var file = new File("/pdf/test.pdf","r");
What's the best way to do it?
Thank you!
I want to convert audio file into base64 using Javascript only.
We can convert images into base64 using canvas. But how can we convert audio files.
Any help will be grateful.
you can give the below code a try, it uses btoa
function getData(audioFile, callback) {
var reader = new FileReader();
reader.onload = function(event) {
var data = event.target.result.split(',')
, decodedImageData = btoa(data[1]); // the actual conversion of data from binary to base64 format
callback(decodedImageData);
};
reader.readAsDataURL(audioFile);
}
Here's how to convert an audio file to a base64 string with JavaScript:
async function audioToBase64(audioFile) {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onerror = reject;
reader.onload = (e) => resolve(e.target.result);
reader.readAsDataURL(audioFile);
});
}
You can use it like this:
<input oninput="audioToBase64(this.files[0]).then(result => console.log(result))" type="file">
This will console.log a string like data:audio/mpeg;base64,//uYxAAAAA... when a file is chosen in that filepicker.
Here i want to get the Data from Excel File by using upload file control.My Snippet is
window.onload = function () {
var fileInput = document.getElementById('fup1');
var fileDisplayArea = document.getElementById('txt1');
fileInput.addEventListener('change', function (e) {
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function (e) {
txt1.innerText = reader.result;
}
reader.readAsText(file);
});
}
when i run this code i get the data in below format
PK!q9+p��[Content_Types].xml ��(�̔MN�0��H�!�%n��j�?K��ؓƪc[���g��
P�T��DQ4���f��|[�d��9g#���Ni�����Cz���*a�����|v~6}�y���-欌��p���J`�
how can i resolve this please help me
First of all you have to understand what you are doing. You are taking a exel file( which is not in a txt format) converting it into fileStream( buffer of bytes) finally you are converting it into txt file( which was a exel file). So what do you expect the result.
Now try Solving this problem using two popular JavaScript libraries:
1. xls
2. xlsx
Which allow you to parse in pure JavaScript.
For Documentation of these two libraries you can refer to following link.
Documentation
Am using the Plupload Uploader , what am trying to implement is after the file is uploaded i wont to convert the file to base64 and send it in a soap envelop , i managed to create an envelop and use my web-service , now my question is how can i get the file as base64 in the uploader.bind 'FileUploaded' :
uploader.bind('FileUploaded', function(up, file) {
}
Best Regards,
This works for me, if using an HTML5 supported browser
UploadFile: function (up, file) {
var reader = new window.FileReader();
reader.readAsDataURL(file.getNative());
reader.onload = function () {
base64data = reader.result;
base64data = base64data.substring(base64data.indexOf(",") + 1);
}
}