Adam Hari
Adam Hari

Reputation: 145

I'm struggling to save an audio file from a web page using Javascript and XMLHTTPRequest

I am trying to build a chrome extension which will download all of the anchor tags which point to a .wav file from a given page. I am using FileSaver.js. My problem is that the downloaded files contain no data and only 'undefined'. My code is as follows (urlList is an array which contains only URLs that end in .wav):

var xhrList = [];
urlList.forEach(function (url, index) {
    xhrList[index] = new XMLHttpRequest();
    xhrList[index].open('GET',url,true);
    xhrList[index].responseType = 'blob';

    xhrList[index].onreadystatechange = function (e) {
        if (this.readyState == 4 && this.status == 200) {

            var blob = new Blob([this.response], {type:'audio/wav'});

            saveAs(blob, url);

        }

    }
    xhrList[index].send();
});

I've tried looking at a few solutions but I haven't had any luck with them. Any help would be greatly appreciated.

Upvotes: 1

Views: 718

Answers (1)

Adam Hari
Adam Hari

Reputation: 145

Reloading the extension did the trick.

Upvotes: 1

Related Questions