2

Unicode Encoding... Gettting UTF-8 I am using this code...

    var content = 'demo';
    uriContent = "data:application/text," + encodeURIComponent(content);
    var link = document.createElement('a');
    link.setAttribute('href', uriContent);
    link.setAttribute('download', '22ddd' + '.txt');
    link.click();

The content in the text file will be in Hindi Language which will be further converted into APS fonts. The font converter software available with the client works when the encoding is Unicode. I am able to download the text file properly but it is in UTF-8 encoding

8
  • 1
    Have you looked at this post? stackoverflow.com/questions/5403912/… Commented Mar 2, 2017 at 11:58
  • 1
    Possible duplicate of create a text file using javascript Commented Mar 2, 2017 at 12:20
  • 1
    @oneNiceFriend: I don't think the questioner wants to use ActiveX to create a textfile. It's old and works only with Internet Explorer. Commented Mar 2, 2017 at 14:03
  • Please can you add to your question what isn't currently working? What's wrong with the encoding? UTF-8 looks like an unicode encoding for me.. Commented Mar 2, 2017 at 14:04
  • 2
    UTF-8 is Unicode. Commented Mar 3, 2017 at 6:19

1 Answer 1

1

The documentation says,encodeURIComponent generates only UTF-8. So we must try something different.

I tried to put some pieces together:

and it works (for me):

<html>
    <body>
        <script>window.TextEncoder = window.TextDecoder = null;</script>
        <script src="encoding-indexes.js"></script>
        <script src="encoding.js"></script> 
        <script>           
            demo = function() {
                var content = 'demo';
                var uint8array = new TextEncoder( 'utf-16', { NONSTANDARD_allowLegacyEncoding: true }).encode(content);
                var blob = new Blob([uint8array], {type : "octet/stream" });
                var url = URL.createObjectURL(blob);
                var link = document.createElement('a');
                link.setAttribute('href', url);
                link.setAttribute('download', '22ddd' + '.txt');
                link.click();       
            };
        </script>
        <button onClick="demo();">
            Test
        </button>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Please note that I don't understand why I have to put uint8array in an array to create the blob. Any explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.