I had a similar use case where I needed to transfer Python scripts from the local PC to the CORE itself. The UI action I defined to do this is listed below. As you suggested, I made use of the input element, type 'file'.
/**
* Custom UI-Action 'ui-upload'.
*
* Description:
* [Add description here]
*/
(function () {
var actions = shmi.pkg("visuals.session.userActions"); //get reference to userActions object
var _ipAddress = "192.168.1.1";
var _username = "";
var _password = "";
var _dir = "dir=scripts";
/**
* UI-Action 'ui-upload' implementation
*
* @params {any[]} parameters configured ui-action parameters
* ---- Initial parameters, needs to be updated manually when changed ----
* @param {string} parameters[0] ipAddress
* @param {string} parameters[1] username
* @param {string} parameters[2] password
*
*/
actions["ui-upload"] = async function (parameters) {
const im = shmi.requires("visuals.session.ItemManager"); //get reference to ItemManager instance
_ipAddress = im.readValue(parameters[0]);
_username = im.readValue(parameters[1]);
_password = im.readValue(parameters[2]);
let input = document.createElement('input');
input.type = 'file';
input.accept = '.py';
input.multiple = true;
input.onchange = async _ => {
let zip = new JSZip();
let files = Array.from(input.files);
files.forEach(f => {
zip.file(f.name, f.text());
});
await zip.generateAsync({ type: "blob" })
.then(blob => {
transfer(blob);
});
};
input.click();
};
async function transfer(blob) {
let response = await axios.post(`https://${_ipAddress}/identity-manager/api/v2/auth/token`, { name: _username, password: _password });
// Add error handling
const token = response.data.access_token;
await axios({
method: "put",
url: `https://${_ipAddress}/solutions/api/v1/solutions/DefaultSolution/configurations/appdata/archive?${_dir}`,
data: blob,
headers: { authorization: 'Bearer ' + token, "Content-Type": "application/json" },
});
// Add error handling
console.log('Transfer success...');
}
}());
I've attached a short video clip that shows its use. Note that I'm making use of the axios and jszip JavaScript libraries, which have to be packaged separately. The WebIQ documentation describes how to package such libraries is great detail.
... View more