如果设置了部分内容标头,则谷歌浏览器未完成请求


Google Chrome not completing request if a Partial Content header is set

我正在尝试使用HTTP/1.1部分内容标头将mp3文件流式传输到SoundManager,以允许对我的媒体文件进行一些保护,并允许用户搜索轨道的不同位置。我的代码适用于IE7-10,Firefox,Safari和Opera,但拒绝在Google Chrome中工作。如果我要删除部分内容标题,它将播放文件,但不允许用户查找。

在Chrome的开发工具中检查网络选项卡时,有2个请求,一个状态为待处理,另一个状态为已取消。 这两个请求的大小均为 13B。我尝试播放的文件是 9.11MB。

下面是我用来设置标头和读取文件的代码。

        $name = $_GET['name'];
        $file_size = filesize($file);
        $file = "C:'xampp'htdocs'..'protected'music'testsong.mp3"; // song, user and all other checks are performed before this to get the file path.
        $etag = md5(uniqid(rand(), true));
        $open_file = fopen($file, 'rb');
        if( !$open_file ) throw new Exception('Could not open file');
        $start = 0;
        $end = $file_size - 1;
        if( isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE']) ) {
            $range = explode('-', substr($_SERVER['HTTP_RANGE'], strlen('bytes=')));
            $start = $range[0];
            if( $range[1] > 0 ) $end = $range[1];
            header('HTTP/1.1 206 Partial Content');
            header('Status: 206');
            header('Accept-Ranges: bytes');
            header('Content-Range: ' . $_SERVER['HTTP_RANGE'] . '/' . $file_size);
            header('Content-Length: ' . ($end - $start + 1));
        } else header('Content-Length: ' . $file_size);
        header('Content-Type: ' . $content_type);
        if( $download ) header('Content-Disposition: attachment; filename="' . $name . '"');
        header('Last-Modified: ' . date('D, d M Y H:i:s 'G'M'T', filemtime($file)));
        header('ETag: "' . $etag . '"');
        header('Expires: 0');
        header('Pragma: no-cache');
        header('Cache-Control: must-revalidate');
        if( $start > 0 ) fseek($open_file, $start);
        $bytes_position = $start;
        while( !feof($open_file) && $bytes_position <= $end ) {
            if( connection_aborted() || connection_status() != 0 ) throw New Exception('Connection Aborted');
            $chunk = 1048000;
            if( $bytes_position + $chunk > $end + 1 ) $chunk = $end - $bytes_position + 1;
            $chunk = fread($open_file, $chunk);
            if( !$chunk ) throw New Exception('Could not read file');
            print($chunk);
            flush();
            $bytes_position += $chunk;
        }
        fclose($open_file);

我很确定您遇到的问题出在内容范围标题上

试试这个

header('Content-Range: bytes ' . $start . '-' . $end . '/' . $file_size);