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?