Working With Files in the Ninox API: Part 2

By Adam Davidson, Director of Product Development

This week we will look at how we can read out files from the file API and display their file names in a formula field in Ninox. It is possible to get the list of files for a record from a Ninox script with the files(record) function. However this will only be possible if you are in the same database as the file record whose file you would like to read. To get files from anywhere, you will need to use the API.

				
					html(" <script>let getFiles=()=>{let headers={'Authorization':'Bearer '+'" + apiKey() + "'};let url='https://api.ninox.com/v1/teams/" + teamId() + "/databases/" + databaseId() + "/tables/" + tableId(this) + "/records/" + Id + "/files';fetch(url,{headers:headers}).then(response=>response.json()).then(files=>{let container=document.getElementById('files_conatiner');let html='';for(let file of files){html+='<div>'+file.name+'</div>'}
container.innerHTML=html})}</script> <style onload='getFiles()'></style><div id='files_conatiner' onclick='getFiles()'>hello</div>
")

				
			

This script will display the files that are attached to a record in a formula field. The fetch() javascript function is used to send a GET request (which is the default method) to the api files endpoint. The function getFiles() is executed as soon as the html is rendered on the page by calling the function in the onload event trigger of an empty style element. Files that are being retrieved from other databases can be accessed by simply modifying the url that the request is sent to and the API key if necessary.
Next time we will take a look at how to download files to a user’s device using the API.

en_USEN