summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/core
diff options
context:
space:
mode:
authorSébastien Theys <seb@odoo.com>2019-05-20 14:12:38 +0000
committerSébastien Theys <seb@odoo.com>2019-05-21 11:37:20 +0000
commit9dec7ba7e9edc905b121d2aa6f6d0922ef4121b3 (patch)
tree7c715b1336c6f854372010d24647c67dedbb483d /addons/web/static/src/js/core
parent923701851e61755a2d0a4df6121fe9b684e13df2 (diff)
[IMP] web, *: add utils to get dataURL (base64) from file/blob
* = partner_autocomplete, point_of_sale, web_editor, website_forum, website_slides This can be used for example to preview an image from a file input field. It can also be used to send the base64 content of the input by RPC instead of having to rely on form submit as POST with multipart/form-data. Technically wrap FileReader.readAsDataURL in a Promise. This will also be used in task-1930726 PR: #33508 Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
Diffstat (limited to 'addons/web/static/src/js/core')
-rw-r--r--addons/web/static/src/js/core/utils.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js
index 44e09933a25..44e17324b85 100644
--- a/addons/web/static/src/js/core/utils.js
+++ b/addons/web/static/src/js/core/utils.js
@@ -279,6 +279,28 @@ var utils = {
return "";
},
/**
+ * Gets dataURL (base64 data) from the given file or blob.
+ * Technically wraps FileReader.readAsDataURL in Promise.
+ *
+ * @param {Blob|File} file
+ * @returns {Promise} resolved with the dataURL, or rejected if the file is
+ * empty or if an error occurs.
+ */
+ getDataURLFromFile: function (file) {
+ if (!file) {
+ return Promise.reject();
+ }
+ return new Promise(function (resolve, reject) {
+ var reader = new FileReader();
+ reader.addEventListener('load', function () {
+ resolve(reader.result);
+ });
+ reader.addEventListener('abort', reject);
+ reader.addEventListener('error', reject);
+ reader.readAsDataURL(file);
+ });
+ },
+ /**
* Returns a human readable number (e.g. 34000 -> 34k).
*
* @param {number} number