Skip to content
Franco Mussopower pages
Code snippet

Get file name and file contents as Byte Array

Copy it with the button on the code. For the why and the walkthrough, see “Upload to a File column on record creation using Power Pages’ Portal Web API”.

Get file name and file contents as Byte ArrayJavaScript
function getFileContentsAndMetadata(entityID) {
    // Get the name of the selected file
    var fileName = encodeURIComponent(document.getElementById('file-upload').files[0].name);
    // Get the content of the selected file
    var file = document.getElementById('file-upload').files[0];
    // If the user has selected a file
    if (file) {
        // Read the file as a byte array
        var reader = new FileReader();
        reader.readAsArrayBuffer(file);
        // The browser has finished reading the file, we can now do something with it...
        reader.onload = function(e) {
            // The browser has finished reading the file, we can now do something with it...
            var fileContent = e.target.result;
            // Run the function to upload to the Portal Web API, passing the GUID of the newly created record and the file's contents and name as inputs
            uploadFile(fileContent,fileName,entityID);
        };
    }
}

Search

Book a discovery call

30 minutes. Bring your trickiest Power Pages problem — I’ll tell you honestly whether I’m the right fit.