Amanda
Amanda

Reputation: 127

how to save a video file on local hard disk from the blob using File Saver in java script?

I have written code to download the video from GoogleCloud storage

var btn = document.getElementById("btn");
btn.onclick = function(){ 
    var url = "https://www.googleapis.com/download/storage/v1/b/test/xyzzzzz.mp4";
    var filename = url.substring(url.lastIndexOf("/") + 1).split("?")[0];
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'https://www.googleapis.com/download/storage/v1/b/test/xyzzzzz.mp4', true);
    xhr.responseType = 'blob';
}

How to get the blob object? and then how do I save to local hard disk using File Saver ? What should I pass to the File Saver Saveas() method. I went through File API documentation but I couldn't get it working. Any suggestions?

Upvotes: 2

Views: 5855

Answers (1)

femmestem
femmestem

Reputation: 621

xhr.response holds the value returned by your callback to Google Storage, so that's what you'd pass in. Filesaver saveas(xhr.response, filename)

You didn't ask about cross-browser compatibility, but you should be aware that blob may not be supported by all browsers. You might want to build a blob instead of setting blob as the response type, like this:

xhr.responseType = "arraybuffer"
blob = new Blob([xhr.response], {type: "video/mp4"});

Upvotes: 1

Related Questions