トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

JavaScript:ファイルを読み込みUint8Arrayを生成する のバックアップ(No.1)


JavaScript?:ファイルを読み込みUint8Arrayを生成する

JavaScript?でファイルを読み込み、Uint8Arrayを生成するサンプルです。

生成したUint8Arrayはコンソールに出力します。

※IEでは動きませんw

コード

<!doctype html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
  <h1>ファイルを読み込み、Uint8Arrayを生成するサンプル</h1>
  <p>生成したUint8Arrayをconsoleに出力します。</p>
  <input id="file" name="file" type="file" />

<script>
window.addEventListener('DOMContentLoaded', function() {
  // ファイルが指定されたタイミングで、その情報を表示
  document.querySelector("#file").addEventListener('change', function(e) {
    // ファイルオブジェクトを取得
    var file = document.querySelector('#file').files[0];
    // FileReaderを生成
    var fileReader = new FileReader();
    // ファイルロードが完了したら実行
    fileReader.onload = function webViewerChangeFileReaderOnload(evt) {
      var buffer = evt.target.result;
      var uint8Array = new Uint8Array(buffer);
      // 生成したUint8Arrayをコンソールに表示
      console.log(uint8Array);
    };
    fileReader.readAsArrayBuffer(file);
  }, true);

});
</script>
</body>

</html>