Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Chooser
![npm](https://img.shields.io/npm/dt/cordova-plugin-chooser) ![npm](https://img.shields.io/npm/v/cordova-plugin-chooser) ![GitHub package.json version](https://img.shields.io/github/package-json/v/cyph/cordova-plugin-chooser?color=FF6D00&label=master&logo=github) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/cyph/cordova-plugin-chooser) ![GitHub top language](https://img.shields.io/github/languages/top/cyph/cordova-plugin-chooser) ![GitHub last commit](https://img.shields.io/github/last-commit/cyph/cordova-plugin-chooser)

# cordova-plugin-chooser

## Overview

File chooser plugin for Cordova.

Install with Cordova CLI:

$ cordova plugin add cordova-plugin-chooser
cordova plugin add cordova-plugin-chooser

Supported Platforms:

* Android

* Browser
* iOS

## API
Expand Down
12 changes: 12 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@
</feature>
</config-file>
</platform>

<platform name="browser">
<config-file target="config.xml" parent="/*">
<feature name="Chooser">
<param name="browser-package" value="Chooser"/>
</feature>
</config-file>
<js-module src="src/browser/ChooserProxy.js" name="ChooserProxy">
<runs/>
</js-module>
</platform>

</plugin>
82 changes: 82 additions & 0 deletions src/browser/ChooserProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}

function getFileInternalBrowser(accept, includeData, successCallback, failureCallback) {
var input = document.createElement('input');
input.type = 'file';
if (accept) {
input.accept = accept;
}

input.addEventListener('change', function (event) {
var file = event.target.files[0];
if (!file) {
if (failureCallback) {
failureCallback('No file selected');
}
return;
}

// Create a blob URL
var blobURL = URL.createObjectURL(file);

// If data inclusion (like base64) is not required, return only the file metadata
if (!includeData) {
var result = {
mediaType: file.type,
name: file.name,
uri: blobURL // Blob URL of the file
};
successCallback(JSON.stringify(result)); // Send as JSON
return;
}

// If includeData is true, process the file as Blob
var reader = new FileReader();

reader.onload = function (e) {
// Prepare the result object here
var arrayBuffer = e.target.result; // This is the ArrayBuffer

// Convert ArrayBuffer to Base64
var base64Data = arrayBufferToBase64(arrayBuffer);

var result = {
data: base64Data, // Store Base64 data
mediaType: file.type,
name: file.name,
uri: blobURL // Use Blob URL
};

if (successCallback) {
successCallback(JSON.stringify(result)); // Send as JSON
}
};

// Now read the file as an arrayBuffer to handle the blob
reader.readAsArrayBuffer(file); // Read the file as an arrayBuffer
});

// Trigger the input click
document.body.appendChild(input);
input.click();
document.body.removeChild(input);
}

module.exports = {
getFile: function (successCallback, failureCallback, accept) {
return getFileInternalBrowser(accept, true, successCallback, failureCallback);
},
getFileMetadata: function (successCallback, failureCallback, accept) {
return getFileInternalBrowser(accept, false, successCallback, failureCallback);
}
};

require('cordova/exec/proxy').add('Chooser', module.exports);