Retrieve the base64 encoded information from a FileEntry in PhoneGap

Currently, I'm facing an issue with Phonegap 2.0 where I am trying to obtain a media encoded as base64 from its URI but have been unsuccessful.

function tryToSend(fileReader) {
    // Unsure about what the parameter is
}

function win(file) {
    alert(file.name + ' ' + file.type); // type shows as undefined at this point
    var reader = new FileReader();
    reader.onloadend = tryToSend;
    var encoded = reader.readAsDataURL(file); // Here, 'encoded' remains undefined
}

function fail(error) {
    console.log(error);
}

function onResolveSuccessCompleted(fileEntry) {
    fileEntry.file(win, fail);
}

function onResolveFailed(error) {
    console.log(error);
}

window.resolveLocalFileSystemURI(MY_FILE_URI, onResolveSuccessCompleted, onResolveFailed);

Despite my efforts, I have not been able to retrieve the base64 encoded data for my file which is essential for sending it in a JSON AJAX call.

I wonder if there might be something wrong with my code or if you have any suggestions on how I can achieve my goal?

Thanks.

Cyril

Answer №1

When using reader.readAsDataURL, there is no need to capture the return value since it is an asynchronous call and does not provide any return value. Your tryToSend function should be structured like this:

function tryToSend(evt) {
    var encoding = evt.target.result;
    // The variable 'encoding' now holds your file in base64 encoded string.
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Strategies for Deactivating a Button While Modifying Checkboxes within a Node JS Table

My dilemma involves a table featuring images and checkboxes. I've been attempting to activate a submit button once any checkbox in the table is checked. Here's a snippet of my table setup: <form action="/slideShowPage" method="GET"> &l ...

Passing HttpClient from app.module to another module and then to a service in Angular

Currently, I am in the process of developing my own Angular NPM Package with the prefix "ngx-*". I have successfully compiled the package and am now integrating it into a new project by utilizing the npm link command. Within the service, there is a constr ...

What could be causing the misalignment of the Datepicker calendar in Material UI?

I have integrated a datepicker using the library "@mui/x-date-pickers/DatePicker". import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment"; import { Locali ...

Saved links and navigating within an iFrame originating from a separate secure domain

Is it possible to execute a bookmarklet and enable navigation on an iFrame sourced from a different secure domain? For instance, suppose I have a webpage loaded from https://example.com, containing an iFrame with its source set to . Whenever I run the boo ...

Sharing images on a web app using Express and MongoDB

My objective is to develop a portfolio-style web application that allows administrators to upload images for other users to view. I am considering using Express and MongoDB for this project. I am uncertain about the best approach to accomplish this. Some ...

Is there a way to retrieve an image from the Youtube search feature within a React application?

async function searchYouTube(query ) { query = encodeURIComponent(query); const response = await fetch("https://youtube-search-results.p.rapidapi.com/youtube-search/?q=" + query, { "method": "GET", maxResult: 2, "headers": { ...

Expanding the functionality of express.Router

Can someone help me with how to extend the functionality of express.Router? I attempted the following: class Test extends express.Router() { }; However, I encountered an error when trying this in Express. Does anyone have a solution for this issue? ...

Twitter authentication failed

I encountered an issue while attempting to utilize the Twitter API for displaying a user's tweets. The error message received was as follows: 06-28 11:40:58.280: E/AndroidRuntime(3191): FATAL EXCEPTION: main 06-28 11:40:58.280: E/AndroidRuntime(3 ...

What is the best way to send the index of an array to a onClick event in ReactJS?

const DisplayInventory = ({ items }) => <div className="row"> {items.map((item, i) => <div className="item" key={"item_" + i}> <div className="card hoverable col s3"> <img onClick={purchase ...

Utilize jQuery animations without the need for using display: block in your

I've been experimenting with .slideToggle() in jQuery to toggle a hidden div from display: none to display: grid. However, it seems like this animation is forcing display: block inline onto the HTML and overriding any other CSS classes. CSS .grid { ...

The onClick function was not recognized as a valid function when it was called

I encountered an error when passing an onClick function as a prop in my React app from one component to another. The error message displayed is Uncaught TypeError: handleOnClick is not a function. Here is the function I am passing: propList = ['a&apos ...

How can I initiate an event in YUI3 framework?

If I have a click event on a link or button, how can I trigger the same click event through another action on the page? let myButton = Y.one('.button'); myButton.on('click', function() { // code }); I noticed YUI3's fire() me ...

Have Vue props been set to null?

Currently, I have a component within my vue.js application that looks like this: export default { props: ['forums'], methods: { increment(forum, index) { ForumService.increment(forum) .then(() => { ...

What is the best way to pass data from a child component to its parent in React?

I have a dynamic table where users can select items from a dropdown menu and add them to the list. Currently, I store the item list in the component's state to render the table dynamically. I want users to be able to click on an item in the table and ...

Patience is key as we await the arrival of the loaded data before making a return in AngularFire 0

I am working on a form that should only be submitted if the user provides a valid access key ($scope.access_key) - and each key can only be used once. Within my controller, I have the following method: $scope.verifyAccess = function() { var ref = new ...

Select a random character from a string using JavaScript

This question sets itself apart from Removing random letters from a string as it focuses on selecting a random letter from a string in JavaScript without removing any characters. The goal is to implement a code that picks random letters from a string in J ...

Having trouble executing the yarn command for clasp login

Issue with running yarn clasp login I am not very proficient in English, so please bear with me. 8> yarn clasp login yarn run v1.22.22 $ C:\Users\myname\Desktop\個人開発プロジェクト\clasp-240418\node_modu ...

Is there a way to determine if two elements in two arrays share the same index position?

As a newcomer to javascript, I am curious about whether there is a method available to determine if an element located within one array shares the same index as another element in a different array. For instance: var a = [4,6,3] var b = [6,6,0] Is there ...

Using JQuery to dynamically swap out the Bootstrap progress bar

I created a progress bar using bootstrap with a default value of 33% and a switch that toggles between all active or inactive. When the switch is activated, I want the progress bar to show a value of 100%, and when it's inactive, a value of 0%. Howev ...

Retrieve the encrypted URL

I'm facing an issue with extracting parameters from an encrypted URL. When using the queryparams function, it only retrieves a portion of the URL after being decrypted. For instance, consider this example URL: http://localhost:4200/househouse? MGRjYjQ ...