# Downloading Latest File from Dashboard

**URL:** <https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [27 April 2022 14:01 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875 "2022-04-27T14:01:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamt](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@adamt](https://discourse.nodered.org/u/adamt)\
**Post date:** [27 April 2022 14:01 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/1 "2022-04-27T14:01:24Z")

</div>

Hey all,

I'm trying to create a simple flow to download the latest .csv file generated to the user's machine.

**Context** : When the user turns logging on, some other flows log external measurements to a local .csv file stored in the folder "/home/pi/log/". The file name varies based on the time they started measuring, but the full path is stored in global variables 'global.a-filename' and 'global.b-filename'.

I've tried integrating a version of [this flow](https://flows.nodered.org/flow/db68bd4934cf46f39e6e453a348bc419) but can't get the flow to work with my variables, as they include the full path and using them without a default path seems to cause issues.

If i remove the path in the variable, leaving just the file name, and insert the path into the base path, I can get it to download. Perhaps I can just edit the string I'm inputting to remove the file path.

Here's an example of my variable:  
`global.a-filename="/home/pi/log/A_Power_Log20220427-11:42:32.csv"`  
And here's what I'm using currently in place of it:  
`"/A_Power_Log20220427-11:42:32.csv"`

Any suggestions? I am by no means a node-red pro, so struggling a bit!

Here's what I've come up with...

```auto
[{"id":"1c47c3c0.9e4ecc","type":"function","z":"af4bb936.890328","name":"Set base path","func":"// using basepath = \"\" doesn't work as I'd hoped\n// example global.a-filename = /home/pi/log/A_Power_Log20220427-11:42:32.csv\n\nvar basePath = \"/home/pi/log/\";\nvar filename = msg.req.params.fn;\n\nmsg.filename = basePath + filename;\nreturn [msg, null];\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1280,"y":1640,"wires":[["ad804a0d.4310e8"],["ea82bad4.101d98"]]},{"id":"ea82bad4.101d98","type":"http response","z":"af4bb936.890328","name":"","statusCode":"","headers":{},"x":1650,"y":1680,"wires":[]},{"id":"7d2d8318.53d4bc","type":"catch","z":"af4bb936.890328","name":"","scope":null,"uncaught":false,"x":1120,"y":1720,"wires":[["bfbc1050.6708a","747edb6e.69d3b4"]]},{"id":"bfbc1050.6708a","type":"function","z":"af4bb936.890328","name":"Set 404","func":"msg.payload = msg.error;\nmsg.statusCode = 404;//resource not found\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1500,"y":1720,"wires":[["ea82bad4.101d98"]]},{"id":"747edb6e.69d3b4","type":"debug","z":"af4bb936.890328","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1150,"y":1760,"wires":[]},{"id":"a32f0ed2.38111","type":"ui_template","z":"af4bb936.890328","group":"dfb4a60f.d788f8","name":"ui_temlplate - present download links on dashboard","order":0,"width":0,"height":0,"format":"<div >\n <a href=\"/A_Power_Log20220427-11:42:32.csv\"> Download File A</a>\n <a href=\"/B_Power_Log20220427-11:42:32.csv\">Download File B</a>\n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","x":1490,"y":1800,"wires":[[]]},{"id":"8d759c0.04f7a68","type":"http in","z":"af4bb936.890328","name":"","url":"/:fn","method":"get","upload":false,"swaggerDoc":"","x":1080,"y":1640,"wires":[["1c47c3c0.9e4ecc"]]},{"id":"ad804a0d.4310e8","type":"file in","z":"af4bb936.890328","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":1490,"y":1620,"wires":[["ea82bad4.101d98"]]},{"id":"dfb4a60f.d788f8","type":"ui_group","name":"Data Export","tab":"48418b79.0f5834","order":1,"disp":true,"width":"12"},{"id":"48418b79.0f5834","type":"ui_tab","name":"Dashboard","icon":"dashboard","order":1}]

```

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [27 April 2022 16:10 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/2 "2022-04-27T16:10:45Z")

</div>

It's not clear to me what you are asking ?

There is no reference to any global vars in this flow ?

I notice that the 2nd output of the function node is never used, so not sure what its for ?

---

<div class="post-metadata">

**Author:** ![adamt](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@adamt](https://discourse.nodered.org/u/adamt)\
**Post date:** [27 April 2022 19:06 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/3 "2022-04-27T19:06:52Z")

</div>

Hey, my apologies. The flow I've included is currently using some fixed text in place of where i'd like the variable to sit, within the UI template node:

_I'd like to change:_  
`<a href="/A_Power_Log20220427-11:42:32.csv"> Download File A</a>`  
_to_  
`<a href=(global.a-filename)> Download File A</a>`

But no amount of formatting or adjustment of the associated variables can get it to behave.

The 2nd output is just a leftover component of the template flow I used to get started

Hope this makes more sense..

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [27 April 2022 23:30 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/4 "2022-04-27T23:30:22Z")

</div>

So why not remove the full path before saving the global, or before passing to file node ?

This will remove **/home/pi/log/** from any payload sent in.

```auto
[{"id":"8eb4572f803114c5","type":"function","z":"b0ac7c4e8a64590a","name":"","func":"msg.payload = msg.payload.replace('/home/pi/log/', '');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1020,"y":640,"wires":[["8a46e7325be84d5d"]]},{"id":"f80811ebe5eda0da","type":"inject","z":"b0ac7c4e8a64590a","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"/home/pi/log/A_Power_Log20220427-11:42:32.csv","payloadType":"str","x":870,"y":640,"wires":[["8eb4572f803114c5"]]},{"id":"8a46e7325be84d5d","type":"debug","z":"b0ac7c4e8a64590a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1190,"y":640,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![adamt](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@adamt](https://discourse.nodered.org/u/adamt)\
**Post date:** [28 April 2022 08:36 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/5 "2022-04-28T08:36:59Z")

</div>

Thanks! I’ll give this a crack. I had wondered if something like this would work, just wasn’t sure if it was the best way to go about it

Much appreciated

---

<div class="post-metadata">

**Author:** ![adamt](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@adamt](https://discourse.nodered.org/u/adamt)\
**Post date:** [28 April 2022 09:53 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/6 "2022-04-28T09:53:50Z")

</div>

Unfortunately after some tinkering, this doesn't work. It seems that it doesn't even reach the function node when '/home/pi/log/' is included. I'm wondering if this is a problem with how the http nodes are set up?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [28 April 2022 14:51 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/7 "2022-04-28T14:51:08Z")

</div>

Can you post the flow you have tried ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 June 2022 14:51 UTC](https://discourse.nodered.org/t/downloading-latest-file-from-dashboard/61875/8 "2022-06-27T14:51:16Z")

</div>

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