如何使用java脚本中的file Saver将视频文件从blob保存到本地硬盘上



我已经编写了从GoogleCloud存储下载视频的代码

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';
}

如何获取blob对象?然后如何使用文件保护程序保存到本地硬盘?我应该向File Saver Saveas()方法传递什么。我浏览了文件API文档,但无法使其工作。有什么建议吗?

xhr.response保存回调返回到Google Storage的值,所以这就是您要传递的值。Filesaver saveas(xhr.response, filename)

您没有询问跨浏览器兼容性,但您应该知道blob可能不是所有浏览器都支持的。您可能想要构建一个blob,而不是将blob设置为响应类型,如下所示:

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

最新更新