2017-03-02 77 views
2

Unicode Encoding... Gettting UTF-8 我使用此代碼...如何使用Unicode編碼創建使用java腳本的文本文件?

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(); 

在文本文件中的內容將在印地文的語言,這將進一步轉化爲APS字體。客戶端可用的字體轉換器軟件在編碼爲Unicode時工作。我能夠正常下載文本文件,但它是UTF-8編碼

+1

你看過這篇文章嗎? http://stackoverflow.com/questions/5403912/create-a-text-file-using-javascript – oneNiceFriend

+1

可能重複的[使用JavaScript創建文本文件](http://stackoverflow.com/questions/5403912/create- a-text-file-using-javascript) –

+1

@oneNic​​eFriend:我不認爲提問者想用'ActiveX'來創建一個文本文件。它很舊,只適用於Internet Explorer。 – ventiseis

回答

1

documentation說,encodeURIComponent只生成UTF-8。所以我們必須嘗試不同的東西。

我試圖把一些拼湊:

和它的作品(對我來說):

<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> 
+0

請注意,我不明白爲什麼我必須在數組中創建'uint8array'來創建blob。任何解釋? – ventiseis

+0

謝謝...它的工作..你保存了我的項目... – ashwini777

相關問題