Access an Asset File in beforeRender



  • Hello,

    We have a set of JSON files in __appDirectory holding report settings and rarely-changing data. One of these files (selected via a req.data property) is read via a custom script's beforeRender that attaches its parsed contents to req.data.

    We would like to maintain these files as assets in studio. Is there a way to access and read an asset in beforeRender ?

    Thank you.



  • It is a bit hidden in the assets docs

    But you can add a code like this to the script

    var data = "{#asset vardata.txt @encoding=string}"
    

    and it will get replaced by assets extension



  • Hello Jan,

    Thank you for your response.

    I've noticed that as well but it is not always vardata.txt. It can be vardata1.txt or vardata2.txt. And 1 and 2 are properties or the request.



  • Ah ok. Unfortunately the assets replacement is static and processed before the script is executed. So there is no way to make it dynamic as you require.
    I've created github task to give it some thoughts.

    The solutions which you can evaluate now:

    1. Load all assets you need into script and choose based on your input dynamically afterwards
    2. Use REST based http request from the script to fetch the asset you need. Something like
    require('request').get(`http://localhost:5488/odata/assets?$filter=name eq '${req.data.par}'`)...
    
    1. Write few lines of [custom extension(https://jsreport.net/learn/custom-extension)

Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.