Upload to WebDav ( nextcloud )

Hello,
I have a upload element. Whenever someone uploads a file weweb automatically uploads it to its CDN.
I want to Upload the file to Nextcloud. For this call I need the file as binary.
I tried to convert the file to a Arraybuffer and it seems to work but when I use the variable its empty.

const file = context.workflow['6666666666-666666-66666-66666'].result;
const reader = new FileReader();
reader.readAsArrayBuffer(file);

Furthermore I noticed that weweb uses axios for its api calls but I cannot require it in the javascript code. I installed it via the npm extension but I do not know how to use it in the javascript action.

This is one of my attempts so far (doesnt work :roll_eyes:):

const fileObject = variables[/* Input File Drop - value */ '66666-66666-66666-66666-value']?.[0];

function fileToArrayBuffer(file, callback) {
    const reader = new FileReader();

    reader.onload = function () {
        const arrayBuffer = reader.result;
        callback(arrayBuffer);
    };

    reader.readAsArrayBuffer(file);
}

return new Promise((resolve, reject) => {
    fileToArrayBuffer(fileObject, function (arrayBuffer) {
        const url =  "https://mynextcloud.fqdn.com/remote.php/dav/files/devben/importantdata/" + file.name;
    

        fetch(url, {
            method: 'PUT',
            headers: {
                'OCS-APIRequest': 'true',
                'Authorization': `Basic aksjhdkashdkashdkhaksdhkajshdkasjhdkjasdhkjashd`,
                'Content-Type':  variables['66666-66666-66666-66666-value'][0]['type'], // Adjust content type based on your requirements
            },
            body: arrayBuffer,
        })
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }
            resolve('File uploaded successfully to Nextcloud!');
        })
        .catch(error => {
            reject(error);
        });
    });
});

Any guidance on resolving these issues would be greatly appreciated.

Please uninstall axios from the npm plugin as it’s already in WeWeb projects. You don’t need to require it, simply call the axios object in your formula.