From 598fda9dfccc1798ee1c577c7914a85d65238fc0 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Mon, 24 Jan 2022 17:07:46 +0100 Subject: [PATCH] Add file utility module. --- assets/js/utils/file.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 assets/js/utils/file.js diff --git a/assets/js/utils/file.js b/assets/js/utils/file.js new file mode 100644 index 00000000..0938f588 --- /dev/null +++ b/assets/js/utils/file.js @@ -0,0 +1,37 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Online Appointment Scheduler + * + * @package EasyAppointments + * @author A.Tselegidis + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.5.0 + * ---------------------------------------------------------------------------- */ + +/** + * File utility. + * + * This module implements the functionality of files. + */ +window.App.Utils.File = (function () { + /** + * Convert a file to a base 64 string. + * + * @param {File} file + * + * @return {Promise} + */ + function toBase64(file) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = () => resolve(reader.result); + reader.onerror = (error) => reject(error); + }); + } + + return { + toBase64 + }; +})();