If your question is only how to turn a file into Base64:
Use the function FileReader#readAsDataURL
to read the file. After reading, you can use FileReader#result
to obtain the result.
see browsers that support
function toBase64() {
// obtém o arquivo
var file = document.querySelector('input').files[0];
// obtém uma instância de FileReader
var fileReader = new FileReader();
// Faz a leitura do arquivo
if (file) {
fileReader.readAsDataURL(file);
}
// Quando concluir a leitura, o resultado é inserido no parágrafo 'output'
fileReader.onloadend = function() {
document.getElementById('output').innerHTML = fileReader.result;
};
}
<input type='file' onchange='toBase64()' />
<p id='output'></p>
On the website of Mozilla Developer Network there is a very interesting example using the result read by FileReader
as an attribute value src
of a img
, follow:
/**
* SRC:
* https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
*/
function previewFile() {
var preview = document.querySelector('img');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
<input type="file" onchange="previewFile()"><br>
<img src="" height="200" alt="Image preview...">
By pseudo-upload I think you mean "read", do you want to access local files using javascript? If this is the case, you use the File + HTML 5 API. Full tutorial here.
– GabrielOshiro