URL call from a dashboard button

I made the below flow some time ago to download a file from my Node-Red server. I just pass the full file path like this: http://192.168.x.x:1880/download?filename=/home/pi/something.csv
Up until now I formatted a 'href' link in a ui-tempate in 'target="blank"' so it downloads in a new window and does not change my dashboard window.

[
    {
        "id": "aa8c482.93734b8",
        "type": "http in",
        "z": "74f191ff.db063",
        "name": "",
        "url": "/download",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 140,
        "y": 3440,
        "wires": [
            [
                "d969ba04.e24028"
            ]
        ]
    },
    {
        "id": "d19cc7d8.646328",
        "type": "http response",
        "z": "74f191ff.db063",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 930,
        "y": 3440,
        "wires": []
    },
    {
        "id": "d969ba04.e24028",
        "type": "function",
        "z": "74f191ff.db063",
        "name": "Get the file name",
        "func": "msg.filename = msg.req.query.filename;\nmsg.contentdisposition = \"attachment; filename=\\\"\" + msg.req.query.filename.replace(/^.*(\\\\|\\/|\\:)/, '') + \"\\\"\";\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 390,
        "y": 3440,
        "wires": [
            [
                "e92381c3.c4cd2"
            ]
        ],
        "outputLabels": [
            "Folder selected"
        ]
    },
    {
        "id": "e92381c3.c4cd2",
        "type": "file in",
        "z": "74f191ff.db063",
        "name": "",
        "filename": "filename",
        "filenameType": "msg",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "x": 580,
        "y": 3440,
        "wires": [
            [
                "99ff4953.d0d5c8"
            ]
        ]
    },
    {
        "id": "99ff4953.d0d5c8",
        "type": "change",
        "z": "74f191ff.db063",
        "name": "Set Headers",
        "rules": [
            {
                "t": "set",
                "p": "headers",
                "pt": "msg",
                "to": "{}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "headers.content-type",
                "pt": "msg",
                "to": "text/csv",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "headers.Content-Disposition",
                "pt": "msg",
                "to": "contentdisposition",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 3440,
        "wires": [
            [
                "d19cc7d8.646328"
            ]
        ]
    }
]

This is all good and well, but I would like to do the same using a dashboard button.
But I have no idea how to do it. I was trying to create a button in ui-tempate and for it to call a redirect function, but I could not make it work. Does anyone have a working example?

If I understand correctly this should work

[{"id":"985a1d1ce6113f18","type":"ui_button","z":"da8a6ef0b3c9a5c8","name":"","group":"2d4fe667.28f8ba","order":17,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"<script>window.location.href = \"https://www.sample-videos.com/csv/Sample-Spreadsheet-10-rows.csv\";</script>","payloadType":"str","topic":"topic","topicType":"msg","x":350,"y":520,"wires":[["04d9330d2a4e87cf"]]},{"id":"04d9330d2a4e87cf","type":"change","z":"da8a6ef0b3c9a5c8","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"template","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":540,"wires":[["f27d36356901af0b"]]},{"id":"f27d36356901af0b","type":"ui_template","z":"da8a6ef0b3c9a5c8","group":"2d4fe667.28f8ba","name":"","order":18,"width":0,"height":0,"format":"","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","className":"","x":700,"y":540,"wires":[[]]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":2,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Click the button and Dashboard should be redirected to download.

Thanks a lot, it has worked with a small modification:
<script>window.open('your_url', '_blank');</script>

The window.location.href worked as well, but after executing it, the dashboard stopped responding. With tine window.open there is a quick flicker as the browser opens a new windows and also closes it as soon as the download begins. But seems to be working and no impact on the dashboard.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.