LoginSignup
14
11

More than 5 years have passed since last update.

JavaScript FileReaderを使って、ファイルをバイナリで読み込む

Last updated at Posted at 2015-06-16

FileReaderを使ってMIDIファイルを読み込む際に勉強したメモです。

readAsArrayBufferメソッドを使う

input.html
<input type=file id=loadFile>
loadMidi.js
//using jQuery
$(function(){
    $("#loadFile").change(function(e){
        var file = e.target.files[0];  
        var reader = new FileReader();

        reader.onload = function() {   
            var ar = new Uint8Array(reader.result);   
        }

        reader.readAsArrayBuffer(file);
    })
})

UnitArrayは通常の配列とはちょっと違います。
配列の要素には添え字でアクセス可能ですが、
shiftとか、joinといったメソッドがありません。

subarray(begin,end)メソッドを使えば、任意のオフセットのデータを取得できます。
詳しくはここ
以上!

14
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11