Created
April 25, 2018 20:19
-
-
Save luisenriquecorona/64b54d5da150a7463948b4f65f585f42 to your computer and use it in GitHub Desktop.
Post & Character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<meta content="IE=edge" http-equiv="X-UA-Compatible" /> | |
<meta charset="utf-8" /> | |
<meta content="width=device-width, initial-scale=1.0" name="viewport" /> | |
<link href="http://cdn.jsdelivr.net/foundation/5.0.2/css/foundation.min.css" rel="stylesheet" /> | |
<h3>Post y Caracteres</h3> | |
<div><label for="textPost">Post:</label> <input id="textPost" type="text" value="Escribe aqui" /><br /> | |
<label for="textCaracter">Caracter</label> <input id="textCaracter" type="text" value="Ingresa Caracteres" /></div> | |
<div><label for="textPost1">Post:</label> <input id="textPost1" type="text" value="Escribe aqui" /><br /> | |
<label for="textCaracter1">Caracter:</label> <input id="texttCaracter1" type="text" value="Ingresa Caracteres" /></div> | |
<div><label for="textPost2">Post:</label> <input id="textPost2" type="text" value="Escribe aqui" /><br /> | |
<label for="textCaracter2">Caracter:</label> <input id="texttCaracter2" type="text" value="Ingresa Caracteres" /></div> | |
<div><label for="textPost3">Post:</label> <input id="textPost3" type="text" value="Escribe aqui" /><br /> | |
<label for="texttCaracter3">Caracter:</label> <input id="textCaracter3" type="text" value="Ingresa Caracteres" /></div> | |
| |
<div style="text-align: center;"> | |
<table border="0" cellpadding="1" cellspacing="1" height="134" style="height:100px;width:100%;" width="1349"> | |
<tbody> | |
<tr> | |
<th style="background-color: rgb(153, 204, 255);"><input class="small radius button" id="boton-txt" type="button" value="Descargar TXT" /></th> | |
<th style="background-color: rgb(153, 204, 255);"><input class="small radius button" id="boton-xml" type="button" value="Descargar XML" /></th> | |
<th style="background-color: rgb(153, 204, 255);"></th> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
<script> | |
function descargarArchivo(contenidoEnBlob, nombreArchivo) { | |
var reader = new FileReader(); | |
reader.onload = function (event) { | |
var save = document.createElement('a'); | |
save.href = event.target.result; | |
save.target = '_blank'; | |
save.download = nombreArchivo || 'archivo.dat'; | |
var clicEvent = new MouseEvent('click', { | |
'view': window, | |
'bubbles': true, | |
'cancelable': true | |
}); | |
save.dispatchEvent(clicEvent); | |
(window.URL || window.webkitURL).revokeObjectURL(save.href); | |
}; | |
reader.readAsDataURL(contenidoEnBlob); | |
}; | |
// | |
function obtenerDatos() { | |
return { | |
post: document.getElementById('textPost').value, | |
caracter: document.getElementById('textCaracter').value, | |
fecha: (new Date()).toLocaleDateString() | |
}; | |
}; | |
function obtenerDatos1() { | |
return { | |
post1: document.getElementById('textPost1').value, | |
caracter1: document.getElementById('textCaracter1').value, | |
fecha: (new Date()).toLocaleDateString() | |
}; | |
}; | |
function obtenerDatos2() { | |
return { | |
post2: document.getElementById('textPost2').value, | |
caracter2: document.getElementById('textCaracter2').value, | |
fecha: (new Date()).toLocaleDateString() | |
}; | |
}; | |
function obtenerDatos3() { | |
return { | |
post3: document.getElementById('textpost3').value, | |
caracter3: document.getElementById('textCaracter3').value, | |
fecha: (new Date()).toLocaleDateString() | |
}; | |
}; | |
// | |
// | |
function escaparXML(cadena) { | |
if (typeof cadena !== 'string') { | |
return ''; | |
}; | |
cadena = cadena.replace('&', '&') | |
.replace('<', '<') | |
.replace('>', '>') | |
.replace('"', '"'); | |
return cadena; | |
}; | |
function obtenerDatos() { | |
return { | |
post: document.getElementById('textPost').value, | |
caracter: document.getElementById('textCaracter').value, | |
fecha: (new Date()).toLocaleDateString() | |
}; | |
}; | |
// | |
function generarTexto(datos) { | |
var texto = []; | |
texto.push('Post y Caracteres:n'); | |
texto.push('Post: '); | |
texto.push(datos.post); | |
texto.push('n'); | |
texto.push('Caracter: '); | |
texto.push(datos.caracter); | |
texto.push('n'); | |
texto.push('Fecha: '); | |
texto.push(datos.fecha); | |
texto.push('n'); | |
texto.push('Post y Caracteres:n'); | |
texto.push('Post1: '); | |
texto.push(datos.post1); | |
texto.push('n'); | |
texto.push('Caracter1: '); | |
texto.push(datos.caracter1); | |
texto.push('n'); | |
texto.push('Fecha: '); | |
texto.push(datos.fecha); | |
texto.push('n'); | |
texto.push('Post y Caracteres:n'); | |
texto.push('Post2: '); | |
texto.push(datos.post2); | |
texto.push('n'); | |
texto.push('Caracter2: '); | |
texto.push(datos.caracter2); | |
texto.push('n'); | |
texto.push('Fecha: '); | |
texto.push(datos.fecha); | |
texto.push('n'); | |
texto.push('Post y Caracteres:n'); | |
texto.push('Post3: '); | |
texto.push(datos.post3); | |
texto.push('n'); | |
texto.push('Caracter3: '); | |
texto.push(datos.caracter3); | |
texto.push('n'); | |
texto.push('Fecha: '); | |
texto.push(datos.fecha); | |
texto.push('n'); | |
// | |
// | |
// | |
return new Blob(texto, { | |
type: 'text/plain' | |
}); | |
}; | |
// | |
function generarXml(datos) { | |
var texto = []; | |
texto.push('<?xml version="1.0" encoding="UTF-8" ?>n'); | |
texto.push('<datos>n'); | |
texto.push('t<post>'); | |
texto.push(escaparXML(datos.post)); | |
texto.push('</post>n'); | |
texto.push('t<caracter>'); | |
texto.push(escaparXML(datos.caracter)); | |
texto.push('</caracter>n'); | |
texto.push('t<fecha>'); | |
texto.push(escaparXML(datos.fecha)); | |
texto.push('</fecha>n'); | |
texto.push('</datos>'); | |
// | |
return new Blob(texto, { | |
type: 'application/xml' | |
}); | |
}; | |
document.getElementById('boton-xml').addEventListener('click', function () { | |
var datos = obtenerDatos(); | |
descargarArchivo(generarXml(datos), 'archivo.xml'); | |
}, false); | |
document.getElementById('boton-txt').addEventListener('click', function () { | |
var datos = obtenerDatos(); | |
descargarArchivo(generarTexto(datos), 'archivo.txt'); | |
}, false); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment