0

I'm trying to get the base64 content from a local audio on my device using javascript. I'm using the following code:

var file = new File(["file_name.mp4"], "/data/user/files/VoiceRecordingPluginData/file_name.mp4");
var reader = new FileReader();

// Read file content on file loaded event
reader.onloadend = function (event) {
    console.log(reader.result);
};

// Convert data to base64
reader.readAsDataURL(file);

As result, "file" gets populated like this: file.name: ["file_name.mp4"] file.localURL: "/data/user/files/VoiceRecordingPluginData/file_name.mp4"

Once "onloaded" is reached, "reader.result" is empty.

Any hints on what's wrong in my code?

  • Hello. Can you explain the application? The HTML5 `file` class expects *user* input not hard coded. You cant arbitrarily access files without "permission" on the user's file system I dont think, even your own. – GetSet Mar 05 '20 at 13:46
  • check this out : https://stackoverflow.com/questions/36280818/how-to-convert-file-to-base64-in-javascript – BilluBaziger Mar 05 '20 at 13:49
  • You can't read a data file from a client's computer like that, nor read a file from a server like that. To read client file you need some type of user interaction like selecting a file in a input file element or drag&drop operation. For server you would need to use ajax – Patrick Evans Mar 05 '20 at 13:51
  • I'm using the Cordova voice recording plugin to build an Android app (WebView). As a result of that plugin interaction, we get an audio that's located on a particular path. I'm new to the javascript world, so my aproach using File and FileReader is wrong then, as i guess from your reply... – rc_nico Mar 05 '20 at 14:17

0 Answers0