Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added browser platform #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added browser platform #52

wants to merge 1 commit into from

Conversation

andreszs
Copy link

@andreszs andreszs commented Oct 8, 2024

Here's a PR that adds the browser platform without editing any of the pre-existing files, except for README.md in which I've added some nice badges like version numbers and code size. It has been tested on the browser v6.0.0 platform.

File Selection Process Overview

The file selection process in the cordova-plugin-chooser follows these steps:

1. Calling getFile()

The process begins when the getFile() function is called. This function is provided by the Chooser module, and it is responsible for initiating the file selection process.

2. Creating a File Input Element

Inside the getFileInternalBrowser function:

  • A new <input> element of type file is created.
  • If an accept parameter is provided, it sets the accepted file types for the input.

3. Adding Event Listener

An event listener is added to the input element:

  • It listens for the change event, which triggers when a user selects a file.

4. Handling File Selection

When a file is selected:

  • The selected file is retrieved from the event.target.files array.
  • If no file is selected, the failure callback is invoked with an error message.

5. Creating a Blob URL

If a file is selected:

  • A Blob URL is created using URL.createObjectURL(file). This URL points to the file's location in memory.

6. Checking for Data Inclusion

The function checks if data inclusion is requested (includeData):

  • If includeData is false, the function constructs a result object containing metadata (e.g., mediaType, name, and uri) and sends it as a JSON string via the success callback.

7. Reading the File as an ArrayBuffer

If includeData is true:

  • A FileReader object is created to read the file as an ArrayBuffer.
  • The onload event of the FileReader is triggered once the file is read successfully.

8. Preparing the Result Object

In the onload handler:

  • A result object is prepared that includes:
    • data: The ArrayBuffer containing the file data.
    • mediaType: The MIME type of the file.
    • name: The name of the file.
    • uri: The Blob URL.

9. Sending the Result

Finally:

  • The result object is serialized to a JSON string and sent back to the success callback.

Summary

This flow ensures that the plugin efficiently handles file selection, allowing developers to obtain both metadata and file data as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant