14/08/2015, 11:54
|
| | Fecha de Ingreso: agosto-2015
Mensajes: 1
Antigüedad: 9 años, 5 meses Puntos: 0 | |
Respuesta: ¿Cómo guardar un TXT en Javascript? hola no me funciona, podrian ayudarme este el codigo que utlizo:
<!DOCTYPE html>
<html>
<script type="text/javascript">
function descargarArchivo(nombreArchivo) {
function saveTextAsFile()
{
// grab the content of the form field and place it into a variable
var textToWrite = document.getElementById("inputTextToSave").value;
// create a new Blob (html5 magic) that conatins the data from your form feild
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
// Specify the name of the file to be saved
var fileNameToSaveAs = "myNewFile.txt";
// Optionally allow the user to choose a file name by providing
// an imput field in the HTML and using the collected data here
var fileNameToSaveAs = txtFileName.text;
// create a link for our script to 'click'
var downloadLink = document.createElement("a");
// supply the name of the file (from the var above).
// you could create the name here but using a var
// allows more flexability later.
downloadLink.download = fileNameToSaveAs;
// provide text for the link. This will be hidden so you
// can actually use anything you want.
downloadLink.innerHTML = "My Hidden Link";
// allow our code to work in webkit & Gecko based browsers
// without the need for a if / else block.
window.URL = window.URL || window.webkitURL;
// Create the link Object.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
// when link is clicked call a function to remove it from
// the DOM in case user wants to save a second file.
downloadLink.onclick = destroyClickedElement;
// make sure the link is hidden.
downloadLink.style.display = "none";
// add the link to the DOM
document.body.appendChild(downloadLink);
// click the new link
downloadLink.click();
}
function destroyClickedElement(event)
{
// remove the link from the DOM
document.body.removeChild(event.target);
}
</sccript>
<script type="text/javascript" src="guardar_textarea.js"></script>
</head>
<body>
<table>
<tr><td>Escriba el texto:</td></tr>
<tr>
<td colspan="3">
<textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
</td>
</tr>
<tr>
<td><button onclick="saveTextAsFile()">Save Text to File</button></td>
</tr>
</table>
</form>
</body>
</html>
alguna idea, muchas gracias |