Ver Mensaje Individual
  #5 (permalink)  
Antiguo 03/12/2009, 23:34
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: Guardar y descargar un archivo .txt proveniente den textarea

En realidad en javascript podés hacer esto (funciona en todos los navegadores excepto en chrome):
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<
title>Documento sin t&iacute;tulo</title>
<
script type="text/javascript">
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
    
// private property
    
_keyStr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
    
// public method for encoding
    
encode : function (input) {
        var 
output "";
        var 
chr1chr2chr3enc1enc2enc3enc4;
        var 
0;
 
        
input Base64._utf8_encode(input);
 
        while (
input.length) {
 
            
chr1 input.charCodeAt(i++);
            
chr2 input.charCodeAt(i++);
            
chr3 input.charCodeAt(i++);
 
            
enc1 chr1 >> 2;
            
enc2 = ((chr1 3) << 4) | (chr2 >> 4);
            
enc3 = ((chr2 15) << 2) | (chr3 >> 6);
            
enc4 chr3 63;
 
            if (
isNaN(chr2)) {
                
enc3 enc4 64;
            } else if (
isNaN(chr3)) {
                
enc4 64;
            }
 
            
output output +
            
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
        }
 
        return 
output;
    },
 
    
// public method for decoding
    
decode : function (input) {
        var 
output "";
        var 
chr1chr2chr3;
        var 
enc1enc2enc3enc4;
        var 
0;
 
        
input input.replace(/[^A-Za-z0-9+/=]/g"");
 
        while (
input.length) {
 
            
enc1 this._keyStr.indexOf(input.charAt(i++));
            
enc2 this._keyStr.indexOf(input.charAt(i++));
            
enc3 this._keyStr.indexOf(input.charAt(i++));
            
enc4 this._keyStr.indexOf(input.charAt(i++));
 
            
chr1 = (enc1 << 2) | (enc2 >> 4);
            
chr2 = ((enc2 15) << 4) | (enc3 >> 2);
            
chr3 = ((enc3 3) << 6) | enc4;
 
            
output output String.fromCharCode(chr1);
 
            if (
enc3 != 64) {
                
output output String.fromCharCode(chr2);
            }
            if (
enc4 != 64) {
                
output output String.fromCharCode(chr3);
            }
 
        }
 
        
output Base64._utf8_decode(output);
 
        return 
output;
 
    },
 
    
// private method for UTF-8 encoding
    
_utf8_encode : function (string) {
        
string string.replace(/rn/g,"\n");
        var 
utftext "";
 
        for (var 
0string.lengthn++) {
 
            var 
string.charCodeAt(n);
 
            if (
128) {
                
utftext += String.fromCharCode(c);
            }
            else if((
127) && (2048)) {
                
utftext += String.fromCharCode((>> 6) | 192);
                
utftext += String.fromCharCode((63) | 128);
            }
            else {
                
utftext += String.fromCharCode((>> 12) | 224);
                
utftext += String.fromCharCode(((>> 6) & 63) | 128);
                
utftext += String.fromCharCode((63) | 128);
            }
 
        }
 
        return 
utftext;
    },
 
    
// private method for UTF-8 decoding
    
_utf8_decode : function (utftext) {
        var 
string "";
        var 
0;
        var 
c1 c2 0;
 
        while ( 
utftext.length ) {
 
            
utftext.charCodeAt(i);
 
            if (
128) {
                
string += String.fromCharCode(c);
                
i++;
            }
            else if((
191) && (224)) {
                
c2 utftext.charCodeAt(i+1);
                
string += String.fromCharCode(((31) << 6) | (c2 63));
                
+= 2;
            }
            else {
                
c2 utftext.charCodeAt(i+1);
                
c3 utftext.charCodeAt(i+2);
                
string += String.fromCharCode(((15) << 12) | ((c2 63) << 6) | (c3 63));
                
+= 3;
            }
 
        }
 
        return 
string;
    }
 
}
function 
salvar(t){
    if(
window.ActiveXObject){
        var 
v=window.open('','pp','');
        
v.document.open();
        
v.document.write(t+'<scr'+'ipt>document.execCommand("SaveAs",null,"archivo.txt");</scr'+'ipt>');
        
v.document.close();
    }else{
        if(
typeof btoa=='function')
            
window.location='data:application/octet-stream;base64,'+btoa(t);
        else
            
window.location='data:application/octet-stream;base64,'+Base64.encode(t);
    }
}

</script>
</head>

<body>
<form id="form1" name="form1">
  <textarea name="textarea" cols="80" rows="5" id="textarea"></textarea>
  <input onclick="salvar(textarea.value)" type="button" name="Submit" value="descargar como txt" id="Submit" />
</form>
<iframe id="pp" name="pp" style="position:absolute; left:-1500px" />
</body>
</html>