Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/10/2014, 04:07
Avatar de HackID1
HackID1
 
Fecha de Ingreso: febrero-2013
Ubicación: En Update
Mensajes: 492
Antigüedad: 12 años
Puntos: 17
Respuesta: ¿Cómo guardar un TXT en Javascript?

Hola!!!.

Si se puede hacer esto en JScript usando Blob HTML5.

Un objeto Blob representa un objeto de fichero de datos brutos, inmutables. Blobs representan datos que no son necesariamente en un formato JavaScript-nativa. La interfaz del archivo se basa en Blob, heredando funcionalidad blob y ampliarlo para soportar archivos en el sistema del usuario. El objeto Blob y la interfaz de archivo son tanto una parte de la WebAPI

Y ahora. Creamos un HTML con el formulario y el textarea.

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <head></title>Guardar Textarea</title>
  3. <script type="text/javascript" src="guardar_textarea.js"></script>
  4. </head>
  5.  
  6.   <tr><td>Escriba el texto:</td></tr>
  7.   <tr>
  8.         <td colspan="3">
  9.             <textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
  10.         </td>
  11.     </tr>
  12.     <tr>
  13.    
  14.     <!-- A button to call out JavaScript Function -->
  15.         <td><button onclick="saveTextAsFile()">Save Text to File</button></td>
  16.     </tr>
  17.  
  18.  
  19.   </body>
  20. </html>

Ahora el Fichero que procesará el textarea y lo guardara en un archivo .txt

El nombre del archivo Js sería: guardar_textarea.js

Código Javascript:
Ver original
  1. function saveTextAsFile()
  2. {      
  3. // grab the content of the form field and place it into a variable
  4.     var textToWrite = document.getElementById("inputTextToSave").value;
  5. //  create a new Blob (html5 magic) that conatins the data from your form feild
  6.     var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
  7. // Specify the name of the file to be saved
  8.     var fileNameToSaveAs = "myNewFile.txt";
  9.    
  10. // Optionally allow the user to choose a file name by providing
  11. // an imput field in the HTML and using the collected data here
  12. // var fileNameToSaveAs = txtFileName.text;
  13.  
  14. // create a link for our script to 'click'
  15.     var downloadLink = document.createElement("a");
  16. //  supply the name of the file (from the var above).
  17. // you could create the name here but using a var
  18. // allows more flexability later.
  19.     downloadLink.download = fileNameToSaveAs;
  20. // provide text for the link. This will be hidden so you
  21. // can actually use anything you want.
  22.     downloadLink.innerHTML = "My Hidden Link";
  23.    
  24. // allow our code to work in webkit & Gecko based browsers
  25. // without the need for a if / else block.
  26.     window.URL = window.URL || window.webkitURL;
  27.          
  28. // Create the link Object.
  29.     downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
  30. // when link is clicked call a function to remove it from
  31. // the DOM in case user wants to save a second file.
  32.     downloadLink.onclick = destroyClickedElement;
  33. // make sure the link is hidden.
  34.     downloadLink.style.display = "none";
  35. // add the link to the DOM
  36.     document.body.appendChild(downloadLink);
  37.    
  38. // click the new link
  39.     downloadLink.click();
  40. }
  41.  
  42. function destroyClickedElement(event)
  43. {
  44. // remove the link from the DOM
  45.     document.body.removeChild(event.target);
  46. }
  47.  
  48. // EOF

El código lo use hace ya tiempo pero funciona perfectamente para lo que quieres realizar.
Por defecto guarda las descargas igual que cuando descargas algo de Internet, en la carpeta Descargas.

Saludos!!!.
__________________
Puntuar +1 es buena forma de dar las gracias. :P
Your Time is limited, so don't waste it living someone else´s life.
Por: HackID1