i have been trying to get the base64 of a file. however i could not use the plugin for some reason.
window.resolveLocalFileSystemUrl(path, gotFile, fail);
This above code gives me a:
"Property 'resolveLocalFileSystemUrl' does not exist on type 'Window'."
error.
Is there any way to fix it? i have the plugin installed. Also, i have tried(from other stackoverflow answers)
window.resolveLocalFileSystemUri(path, gotFile, fail);
window.resolveLocalFileSystemURL(path, gotFile, fail);
window.resolveLocalFileSystemURI(path, gotFile, fail);
OR if there is another way to retrieve the base64, please assist.
By the way, im using filechoose to open and choose the file.
i have solved the issue by re-installing the plugin.
getFileContentAsBase64(path, callback){
window.resolveLocalFileSystemURL(path, gotFile, fail);
function fail(e){
alert('Cannot found requested file');
}
function gotFile(fileEntry){
fileEntry.file(function (file){
var reader = new FileReader();
reader.onloadend = function(e){
var content = this.result;
callback(content);
}
reader.readAsDataURL(file);
});
}
}
the above code allows you to convert a dataURL(file/image/pdf anything) into base64.
you can call it by:
getFileContentAsBase64(obj.toInternalURL().toString(), function (base64File) {
console.log(base64file);
}
Thanks everyone for the help!
I haven't found anywhere default function name window.resolveLocalFileSystemUri it's a resolveLocalFileSystemURL
Try Below
resolveLocalFileSystemURL(path, function(entry) {
var nativePath = entry.toURL();
console.log('Native URI: ' + nativePath);
document.getElementById('image').src = nativePath;
});
Source is here
Make sure you have installed file-transfer plugin.
Related
EDIT : I just created a new Meteor Project and it worked :D wow.But it still doesnt work on my core project..looks like i have different settings.
In my Meteor.js project i have 4 .mp3-files located in public/sounds/xyz.mp3.
I load these .mp3 with :
let soundRequest = new XMLHttpRequest();
soundRequest.open('GET', this._soundPath, true);
soundRequest.responseType = 'arraybuffer';
let $this = this;
soundRequest.onload = function () {
Core.getAudioContext().decodeAudioData(soundRequest.response, function (buffer) {
$this.source.buffer = buffer;
$this.source.loop = true;
$this.source.connect($this.panner);
});
};
soundRequest.send();
This WORKS on google Chrome, but when i build the app via meteor run android-device, i get the following error message : DOMException: Unable to decode audio data
I wonder if this is a bug because loading .png or .jpg works just fine in the mobile version. I have not installed any packages beside meteor add crosswalk but deinstalling this doesnt help either.
You shouldn't need to do a http request to get a local resource. You can just refer to a local url. On the Android device the path is different. See this code:
function getSound(file) {
var sound = "/sounds/"+file;
if (Meteor.isCordova) {
var s;
if (device.platform.toLowerCase() === "android") {
sfile = cordova.file.applicationDirectory.replace('file://', '') + 'www/application/app' + sound;
}
else {
sfile = cordova.file.applicationDirectory.replace('file://', '') + sound;
}
var s = new Media(
sfile,
function (success) {
console.log("Got sound "+file+" ok ("+sfile+")");
s.play();
},
function (err) {
console.log("Get sound "+file+" ("+sfile+") failed: "+err);
}
);
} else {
var a = new Audio(sound);
a.play();
}
}
On a device it loads the sound file asynchronously and then plays it. In the browser it just loads and plays it synchronously.
This web API is not supported on android device but works on chrome browser of android
Check browser specification in this link
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/decodeAudioData
I'm tearing my hair out trying to get the Cordova File Plugin and Cordova File Transfer to work, My code (below) should create a new folder 'Makes' and download an image into that new folder, but instead it just throws error code 1, but I can't figure out why, can someone please point me in the right direction:
function onAppReady(){
if(navigator.splashscreen && navigator.splashscreen.hide){
navigator.splashscreen.hide();
}
updateImages();
}
function updateImages(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(fileSystem){
var directoryEntry=fileSystem.root;
directoryEntry.getDirectory("Makes", {create:true, exclusive:false}, onDirectorySuccess, onDirectoryFail);
}
function onDirectorySuccess(parent){
var rootdir = fileSystem.root;
var fp = parent.toURL();
var download_link=encodeURI('https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1122px-Wikipedia-logo-v2.svg.png');
var fpFull=fp+'Makes/Wikipedia.png';
filetransfer(download_link, fpFull);
}
function onDirectoryFail(error){alert("Unable to create new directory: "+error.code);}
function fileSystemFail(evt){alert(evt.target.error.code);}
}
function filetransfer(download_link, fp){
var fileTransfer=new FileTransfer();
fileTransfer.download(download_link, fp, function(entry){
console.log("download complete: "+entry.fullPath);
},
function(error){
console.log("download error: "+error.code+' - '+error.source+' - '+error.target);
});
}
document.addEventListener("app.Ready", onAppReady, false);
I am trying to create a file in the PERSISTENT (sd card) storage using cordova's api in javascript. I am able to create the file, but unable to write data to it (data is empty on read). Below is my JS code:
$(document).ready(function() {
document.addEventListener("deviceready", onDeviceReady, false);
});
function onDeviceReady() {
writeTestList();
}
function writeTestList() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystemForWrite, function() { showMsg('Unable to access file system for write'); });
}
function gotFileSystemForWrite(fileSystem) {
fileSystem.root.getFile('testList.txt', {create: true, exclusive: false}, gotFileEntryForWrite, function() { showMsg('Unable to access file for write'); });
}
function gotFileEntryForWrite(fileEntry) {
fileEntry.createWriter(gotFileWriter, function() { showMsg('Unable to create test list'); });
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
// show a message
alert('created the test lists');
readSavedContent();
};
writer.onerror = function(evt) {
alert(JSON.stringify(evt));
};
writer.write("raj");
alert(writer.length);
}
The control always goes inside onerror of writer and on alerting the event obj, i see a type: "error", error: "JSON error". I completely don't get this. I am writing to a txt file, but getting json error. I have put write permissions in android manifest. Also, I have storage plugin for cordova enabled in config.xml preferences. People, pls help me out with this..!!
Unable to fix this issue in cordova 3.0. Moved to cordova 2.9 and everything works perfectly.
I am using Phonegap file upload to upload SVG files to my server. It's working fine. But I need to take all SVG files from the iPad's absolute path and send them to my server. I don't know how to get all .svg files using Phonegap's file API, so that I can send to the server looping through file names. Please tell me how to do this.
My code for file upload is:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("image5_2.jpg.svg", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
var localpath=fileEntry.fullPath;
uploadPhoto(localpath);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
var ft = new FileTransfer();
ft.upload(imageURI, "http://192.168.1.54:8080/POC/fileUploader", win, fail, options);
}
You need to create a DirectoryReader from the root FileSystem and loop through all the entries looking for .svg files.
function gotFS(fileSystem) {
var reader = fileSystem.root.createReader();
reader.readEntries(gotList, fail);
}
function gotList(entries) {
var i;
for (i=0; i<entries.length; i++) {
if (entries[i].name.indexOf(".svg") != -1) {
uploadPhoto(entries[i].fullPath);
}
}
}
You may have to make some minor edits to this code but it should get you started.
I am trying to follow the example that is provided on PhoneGap's documentation, except instead of calling the window.requestFileSystem... inside the onDeviceReady, I am calling that whenever I actually need to access a certain file. For some reason, my code does not seem to go past that line.
function pullSelectRecord(link)
{
selectedFile = link;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
alert("this is" + selectedFile);
fileSystem.root.getFile(link, null, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readDataUrl(file);
readAsText(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
alert(evt.target.result);
};
reader.readAsText(file);
}
You can call requestFileSystem from anywhere. If you are not getting past that line you'll need to look in "adb logcat" to see what the error could be. Also, make sure your Manifest.xml has the write external storage permission.