partially download a file with Javascript

左心房为你撑大大i 提交于 2019-11-28 11:26:45

You can't do that with just JavaScript. You'll need something on the server (what exactly depends on your server-side technology) to read the relevant part of the file. Then in JavaScript you can just call that server-side part.

Update 1

The HTTP protocol has support for downloading files in parts (which is what the). But of course HTTP knows nothing of MP3 files, so you'll have to know what range of the file contains the ID3 tag in your JavaScript.

Update 2

It seems way more feasible than I initially expected to download just a chunk of a URL in pure JavaScript:

xhr = new XMLHttpRequest();
xhr.open("GET", "http://<your domain>/");
xhr.setRequestHeader("Range", "bytes=-256");
xhr.send();

This requests the last 256 bytes of the remote file.

Note that of course your request is subject to the usual same-origin limitations, so you can only use it to retrieve files from the server that contains your original HTML/JavaScript.

Withouth using a server-sided programming language: no.

You could use Ajax and PHP (or any other programming language) to get the ID3 tag.

With jQuery it looks something like this:

function getInfo(func) {
$.ajax({
    url: "mp3info.php?file=" + url,
    dataType: "json",
    ready: function(result) {
        func(result);
    }
});
}

And you use it like this:

getInfo(function(mp3info) {
    alert(mp3info.id3);
});

Then your PHP file looks something like this:

<?php
$info = array(); //The MP3 info array
echo json_encode($info, true);
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!