-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallFiles.html
More file actions
66 lines (55 loc) · 2.44 KB
/
allFiles.html
File metadata and controls
66 lines (55 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<html>
<head></head>
<body>
<div>
<div>
<label for="subirArchivo">Buscar archivo:</label><br>
<input type="file" id="subirArchivo">
</div>
<br>
<div>
<h1>Convertir Base64</h1>
<textarea id="base64textarea" placeholder="Base64 will appear here" cols="50" rows="15"></textarea>
<img id="foto" src="" alt="">
<br>
<a id='archivo' style="display:none;"></a>
<a href="#" onclick="downloadPDF()" title='Cotización.pdf'>Has clic para descargar pdf</a>
</div>
</div>
<script>
//Obtener el binario correcto de cualquier archivo (DOC, EXCEL, PDF , ETC)
var obtenerBinario = function (evt) {
debugger;
var files = evt.target.files;
var file = files[0];
var extensionFile = file.name.split(".")[1];
localStorage.setItem("extensionFile", extensionFile);
localStorage.setItem("type", file.type)
if (files && file) {
var reader = new FileReader();
reader.onload = function (readerEvt) {
var binaryString = readerEvt.target.result;
document.getElementById("foto").src = "data:" + localStorage.getItem("type") + ";base64," + btoa(binaryString); // Se asignan los datos de base 64
document.getElementById("base64textarea").value = "data:" + localStorage.getItem("type") + ";base64," + btoa(binaryString);
};
reader.readAsBinaryString(file);
}
};
//Descarga correcta de cualquier archivo, convertido en binario
downloadPDF = function downloadPDF() {
var dlnk = document.getElementById('archivo');
dlnk.setAttribute("download", "archivo." + localStorage.getItem("extensionFile"));
dlnk.href = document.getElementById("base64textarea").value
dlnk.click();
parent.window.open( document.getElementById("base64textarea").value,"blank");
alert('Descarga');
}
//Se agrega el evento de subir archivo
if (window.File && window.FileReader && window.FileList && window.Blob) {
document.getElementById('subirArchivo').addEventListener('change', obtenerBinario, false);
} else {
alert('The File APIs are not fully supported in this browser.');
}
</script>
</body>
</html>