JavaScript Upload
If you are developing a custom application that is handling files in the browser via JavaScript, you can use the JavaScript API of the widget to manually upload files.
The field supports file uploads form an URL String a Blob or a base64 encoded string.
Upload API
UploadField.uploadFile(file: URL|Blob|Base64): Promise<File>
The API returns a Promise that resolves with a file domain object that contains information about the file storage location.
Note: for the upload field to link the uploaded files with an order, the upload field needs to be placed inside a <form action="/cart/add"></from> . (This is done automatically on product pages)
To get access to the upload field instance:
var field; // listener to get a reference to the upload field window.addEventListener('upload:ready', function(e) { // store field reference field = e.detail; });
URL Example
field.uploadFile('https://img.icons8.com/color/48/000000/tesla-logo.png') .then(function(file) { console.debug('got file upload: ', file); });
Blob Upload Example
/* helper function */ function urlToBlob(url) { return new Promise(function(resolve, reject) { try { var xhr = new XMLHttpRequest(); xhr.open("GET", url); xhr.responseType = "blob"; xhr.onerror = function() {reject("Network error.")}; xhr.onload = function() { if (xhr.status === 200) {resolve(xhr.response)} else {reject("Loading error:" + xhr.statusText)} }; xhr.send(); } catch(err) {reject(err.message)} }); } urlToBlob('https://img.icons8.com/color/48/000000/tesla-logo.png').then(function(blob) { field.uploadFile(blob).then(function(file) { console.debug('got file upload: ', file); }); })
Base64 Example
field.uploadFile('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABmJLR0QA/wD/AP+gvaeTAAAEYUlEQVRoge2ZT2hcVRTGf+cmnaRNmwRLacm8MaFEiZRWUaiIVEyL5I+gdVP30m13gkvFjUtBlyKuzUYUnCSUGqmI1KAmrSGpGTFx3hsbQzVJjclM8u5xMZM6mcyfN/Y9SDAfvMW8+91zznfv/e593IF97OP/DQlK/KSrq/2g75/G2i4ViQMdAgmFo4i0oNquqi0iEqsWR1VzIrKKyBKqqwJ3FdIKnlHNaEPDL+vG/PjK3NzSfxYwdOpUrHVp6VmF8xjzOKpngM4A8XxgpQanFWgIEGsekZtq7YSBsZX29q8vTU3lSknbBCQTiddE9SXgAnC4hLsGTCJyG9U5RObE2nlVzciBAyu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2bYcaajzFHzavFBIKoTGuINRDlEKkDz/6ntYQEiE2Yvz0CjyKQ1JlIB+9jHA+IfdfGq5Jus1P8AAAAASUVORK5CYII=') .then(function(file) { console.debug('got file upload: ', file); });