-2

I have a file.mp3 file right now. I need to convert it into a format of byte array. Can anyone tell me how to do it?

Question extension: The file is not limited to .mp3 format?

In addition to byte array, I have some other options as well, e.g.

  1. an Object like { base64: "..." } with a base64-encoded String
  2. a Buffer in Node.js runtime.
  3. a Stream in Node.js runtime.
Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
tutlerock
  • 1
  • 1
  • 1

3 Answers3

0

If doing from a User's upload:

//<![CDATA[
/* external.js */
var doc, bod, I, old = onload;
onload = function(){
if(old)old(); // change old var name if using technique on other pages
doc = document; bod = doc.body;
I = function(id){
  return doc.getElementById(id);
}
function readAsBytes(file, done, doneContext){
  var fr = new FileReader, c = doneContext || this;
  fr.onload = function(){
    done.call(c, fr.result.replace(/^data.+,/, ''));
  }
  fr.readAsDataURL(file);
}
var frm = I('frm'), out = I('out');
frm.onsubmit = function(){
  // stuff you need to happen - use AJAX
  return false;
}
I('up').onchange = function(){
  readAsBytes(this.files[0], function(res){
    // ArrayInstance assignment to .innerHTML automatically does ArrayInstance.join(',')
    out.innerHTML = res.split(''); // don't use split, if you don't want an Array
  });
}
}
//]]>
/* external.css */
html,body{
  padding:0; margin:0;
}
body{
  background:#000; overflow-y:scroll;
}
.main{
  width:936px; background:#fff; padding:20px; margin:0 auto;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <meta name='viewport' content='width=device-width' />
    <title>readAsBytes()</title>
    <link type='text/css' rel='stylesheet' href='external.css' />
    <script type='text/javascript' src='external.js'></script>
  </head>
<body>
  <div class='main'>
    <form id='frm' name='frm'>
      <input id='up' name='up' type='file' />
    </form>
    <div id='out'></div>
  </div>
</body>
</html>
StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • Thanks for your answer. It works for the case of user uploading file. But in my case, it is on the cellphone. I build an app with Ionic3 framework. I need to do an audio recording, so I created an audio file of type "MediaObject", then I need to transfer this "MediaObject" into one of the above forms I listed. So there won't be a file uploading input like in the browser. – tutlerock Dec 19 '17 at 19:16
  • 1
    Thanks, I just solved the problem two minutes ago. I transform it first into binary string, and then I use base64 to encode it, finally I can successfully send the audio. It cost me two whole days. – tutlerock Dec 20 '17 at 03:53
0

I finally solved the problem. Instead of byte array, I go with another option, the base64 encode string. So first, I use an Ionic plugin to transform it into binary string (I think javascript already has an API FileReader.readAsBinaryString() which can do the same thing ); second I simply use btoa() to transform the binary string into base64 encode string.

tutlerock
  • 1
  • 1
  • 1
-1

Check here:

Local file access with javascript

The straight answer is that JavaScript itself doesn't contain file access, but when you program in JavaScript, you aren't programming in JUST JavaScript, but also against some back-end. (Perhaps Chrome, perhaps IE, perhaps Node.JS)

The file access is not part of JavaScript, but of the back-end, and how it works will vary from one environment to the next.

If the other question I mentioned doesn't have your answer, you'll need to write back with what browser or other framework you're running JavaScript in.

NeedsAnswers
  • 169
  • 5
  • Hi, I'm working with Ionic3 framework to build a mobile app. Ionic3 is based on angularJS and javascript. So in my situation, I need to do audio recording. So I created an audio file which is "MediaObject" type, then I need to transfer this "MediaObject" into one of the forms I listed above. – tutlerock Dec 19 '17 at 19:22