Visualizzazione dei risultati da 1 a 4 su 4

Visualizzazione discussione

  1. #3
    Utente di HTML.it L'avatar di U235
    Registrato dal
    Mar 2006
    Messaggi
    1,539
    Ciao,
    probabilmente non ho capito cosa vuoi fare, ma a me non sembra impossibile farlo in javascript
    Una cosa del genere:
    codice:
    <textarea id='txtArea'>Ciao da U235!</textarea>
    ....
    
    var txtArea = document.getElementById('txtArea');
    var txt = txtArea.value;
    var fileB64 = window.btoa(txt);
    var saveFile = (function () {
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.style = "display: none";
                return function (data, name) {
                    var blob = new Blob(data, { type: "octet/stream" }),
                        url = window.URL.createObjectURL(blob);
                    a.href = url;
                    a.download = name;
                    a.click();
                    window.URL.revokeObjectURL(url);
                };
            }());
    
    
    //trasformi in array di byte 
    var binFile = window.atob(fileB64);        
    var bytes = new Uint8Array(binFile.length);
    for (var i = 0; i < binFile.length; i++) {
      var ascii = binFile.charCodeAt(i);
      bytes[i] = ascii;
    }
    
    
    //fai aprire la finestra di download
    saveFile([bytes], 'test.txt');
    Qui un esempio funzionante in chrome.

    P.S.
    Volendo se si tratta di testo puoi anche non trasformarlo in base 64:
    codice:
    var txtArea = document.getElementById('txtArea');
    var txt = txtArea.value;
    var saveFile = (function () {
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.style = "display: none";
                return function (data, name) {
                    var blob = new Blob(data, { type: "octet/stream" }),
                        url = window.URL.createObjectURL(blob);
                    a.href = url;
                    a.download = name;
                    a.click();
                    window.URL.revokeObjectURL(url);
                };
            }());
    
    
    //trasformi in array di byte  
    var bytes = new Uint8Array(txt.length);
    for (var i = 0; i < txt.length; i++) {
      var ascii = txt.charCodeAt(i);
      bytes[i] = ascii;
    }
    
    
    //fai aprire la finestra di download
    saveFile([bytes], 'test.txt');
    Ultima modifica di U235; 01-09-2021 a 19:18

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.