Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

partially download a file with Javascript

we're actually working on a Remote Music Library organizer using javascript and I'd like to know if there's a way to download using js the last 128bytes of an MP3 file in order to get its ID3 Tags. thanks.

like image 420
coolcoolcool Avatar asked Dec 04 '12 14:12

coolcoolcool


2 Answers

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.

like image 117
Frank van Puffelen Avatar answered Oct 28 '22 01:10

Frank van Puffelen


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);
?>
like image 33
Dillen Meijboom Avatar answered Oct 28 '22 03:10

Dillen Meijboom