0

コンテンツが GBK でエンコードされている他のサイトからテキストを読み込みたいのですが、私のサイトは UTF8 です。

これらの GBK テキストを UTF8 に変換して表示する方法はありますか?

何らかの理由で、これには JavaScript しか使用できません。

4

2 に答える 2

1

http://updates.html5rocks.com/2014/08/Easier-ArrayBuffer---String-conversion-with-the-Encoding-API

chrome または firefox の場合、TextDecoder を使用して任意のテキストを Unicode にデコードできます。

function fetchAndDecode(file, encoding) {  
    var xhr = new XMLHttpRequest();  
    xhr.open('GET', file);  
    xhr.responseType = 'arraybuffer';  
    xhr.onload = function() {  
      if (this.status == 200) {  
        var dataView = new DataView(this.response);  
        var decoder = new TextDecoder(encoding);  
        var decodedString = decoder.decode(dataView);  
        console.info(decodedString);  
      } else {  
        console.error('Error while requesting', file, this);  
      }  
    };  
    xhr.send();  
  }

fetchAndDecode('gbkencoded.txt','gbk'); 
于 2015-02-15T11:51:03.123 に答える