-1

i have an m3u file that i wanna extract information from it. can i read as a text file? Any idea how to read a text file or m3u deployed in a server in JavaScript or HTML line by line and get information from it ?

  • You're going to need some server side code. What kind of server is it on? – Matt Spinks Feb 21 '17 at 18:36
  • You want to read it on the client or server? If client, you can use jquery's get function to download it by giving it's URL and split it. If server, then we need to ask what are you using as the backend. – Bulent Vural Feb 21 '17 at 18:39
  • Are you just trying to read a text file using javascript? http://stackoverflow.com/questions/23331546/how-to-use-javascript-to-read-local-text-file-and-read-line-by-line – Emeria Feb 21 '17 at 19:03

2 Answers2

0

I would recommend using jQuery for this.

If you're trying to read the file line by line then use:

<script>
    $.get("item.txt", function(data) {

    });
</script>

Where the argument data is the resultant file information. Even if you are just trying to get the data byte by byte this would work fine. I am not super familiar with .m3u files, but this advice should help.

Neil
  • 1
  • 2
0

I created a basic example using just Javascript, starting with the example that I linked in the comments above. Just read in the text file using FileReader and create a loop in the onload function, where you parse out the records that you want. I based my file spec off of wikipedia, using the Example 6.

Result: https://jsfiddle.net/1sce9mv6/6/

Javascript:

   document.getElementById('file').onchange = function() {

   var file = this.files[0];

   var reader = new FileReader();
   reader.onload = function(progressEvent) {
     // Entire file for debugging
     //console.log(this.result);

     // By lines
     var lines = this.result.split('\n');

     console.log('File read.');

     //Check if this is an m3u file
     if (lines[0].includes("#EXTM3U")) {
       console.log("File header found!");

       //Go through each line
       for (var line = 1; line < lines.length; line++) {

         //Process line
         if (lines[line].includes("#EXTINF")) {

           //print full line
           console.log(lines[line]);

           //split each line into the elements
           var currentLine = lines[line].substring(7).split(',');
           console.log("Runtime: " + currentLine[0]);
           console.log("Song:" + currentLine[1]);
         } else {
           continue;
         }
       }
     } else {
       console.log(lines[0]);
       console.log("Not m3u file...");
     }
   };
   reader.readAsText(file);
 };

HTML:

<input type="file" name="file" id="file">

References:

Community
  • 1
  • 1
Emeria
  • 168
  • 1
  • 14
  • thank you very much, but how can i update this code when my m3u file is online for example: http://www.hello.com/*************.m3u – Wassim Ben Hamida Feb 22 '17 at 16:50
  • Are you asking how to download files? You should create a new question, if that is what you want. Also try to give examples and what you have tried already. It is tough to understand what you are asking. – Emeria Mar 03 '17 at 21:02