Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 强制下载音频流_Javascript_Jquery_Html - Fatal编程技术网

Javascript 强制下载音频流

Javascript 强制下载音频流,javascript,jquery,html,Javascript,Jquery,Html,我有来自外部服务器的音频流,无法执行任何服务器端更改。它目前工作得很好,但它只能在HTML5音频元素中播放歌曲。我还想实现一个下载功能,问题是,链接不会下载任何东西,它会自动流式播放mp3。HTML5下载属性没有任何作用 服务器提供的完整响应标头: Accept-Ranges:bytes Cache-Control:no-cache, no-store, must-revalidate Connection:close Content-Length:6991749 Content-Type:au

我有来自外部服务器的音频流,无法执行任何服务器端更改。它目前工作得很好,但它只能在HTML5音频元素中播放歌曲。我还想实现一个下载功能,问题是,链接不会下载任何东西,它会自动流式播放mp3。HTML5下载属性没有任何作用

服务器提供的完整响应标头:

Accept-Ranges:bytes
Cache-Control:no-cache, no-store, must-revalidate
Connection:close
Content-Length:6991749
Content-Type:audio/mpeg
Date:Sun, 28 Apr 2013 19:40:49 GMT
Expires:Sat, 26 Jul 1997 05:00:00 GMT
Last-Modified:Thu, 10 Jan 2013 18:59:31 GMT
Server:nginx/1.0.2

如果没有PHP代理,它应该可以完全在JavaScript中工作。

事实证明,我最初的悲观(关于这是不可能的)可能误导了我。这是一个演示,它使用了一些HTML5文件API(没有我读到的一些东西那么先进,但似乎安全限制使得目前完全自动化这一点有点遥不可及,在Firefox和Chrome中可以工作,但我想只有IE10有机会支持它

var createObjectURL = function (file) {
    if (window.webkitURL) {
        return window.webkitURL.createObjectURL(file);
    } else if (window.URL && window.URL.createObjectURL) {
        return window.URL.createObjectURL(file);
    } else {
        return null;
    }
},
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png', true);
xhr.responseType = 'blob';

xhr.onload = function (e) {
    if (this.status == 200) {
        var url = createObjectURL(new Blob([this.response], {
            type: 'image/png'
        }));
        var link = document.createElement('A');
        link.setAttribute('href', url);
        link.setAttribute('Download', 'Name I Want it to have.png');
        link.appendChild(document.createTextNode('Download'));
        document.getElementsByTagName('body')[0].appendChild(link);

    }
};
xhr.send();
在这里,我使用
XMLHttpRequest
请求一个PNG,然后将其存储在
Blob
中,并创建一个下载URL,其中包含一些额外的信息,触发浏览器下载资产,而不仅仅是链接到它(这是PNG的功能)。我找不到一个不需要会话密钥的Grooveshark URL,因此我不知道这是否适用于您,但这一限制来自Groovshark阻止歌曲热链接的努力。

我现在就这么做了, 问题是,服务器在被请求一次时自动拒绝访问临时创建的流URI。我只是注释掉了HTML5音频元素,只放置了一个带有新HTML5下载属性的下载链接

<a href="stream.com/stream.mp3" download="filename.mp3">

如果我没有设置“下载”属性,浏览器会自动流式传输文件。
对HTML5竖起大拇指!

你是热链接还是什么?我是流媒体还是你想知道什么?(对不起,我的英语不是最好的)谢谢,看起来很不错!问题是,由于访问控制允许原始规则,该页面也不允许我使用任何XMLhttpRequests。也许Flash会这样做?编辑:它现在可以工作了,HTML5下载属性非常有用,我会写自己的答案!