# Unable to download files from the browser

**URL:** <https://discourse.nodered.org/t/unable-to-download-files-from-the-browser/51314>\
**Category:** General\
**Created:** [21 September 2021 01:15 UTC](https://discourse.nodered.org/t/unable-to-download-files-from-the-browser/51314 "2021-09-21T01:15:44Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 September 2021 12:39 UTC](https://discourse.nodered.org/t/unable-to-download-files-from-the-browser/51314/6 "2021-09-21T12:39:01Z")

</div>

> [@el\_pablo](#):
>
> In this example, the user needs to know what is the name of the file. In my case, there are new files generated everyday. I just want to list the directory content

Then you will need to list the files in the directory & present them as download links.

Something like...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f05655456f2dfbf0517745a2f153071ed358ad44.png)

Result...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/4/b4ab6a15904e7c0ddde223f3b1928881f3f8f6df.png)

Flow...

```auto
[{"id":"f31a598d.9fd2c8","type":"function","z":"553814a2.1248ec","name":"Set base path","func":"//restrict to c:\\temp\\\nvar basePath = flow.get(\"basePath\");\nvar filename = msg.req.params.fn;\n\n\nif(filename.includes(\"..\\\\\")){\n msg.payload = \"Illegal file path\";\n msg.statusCode = 405;//not allowed\n return [null, msg];//fire output 2\n} else if(filename.includes(\"../\")){\n msg.payload = \"Illegal file path\";\n msg.statusCode = 405;//not allowed\n return [null, msg];//fire output 2\n} \n//TODO: add more checks\n\nmsg.filename = basePath + filename;\nreturn [msg, null];//fire output 1\n\n\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1300,"y":2380,"wires":[["34dc99e5.495466"],["98261154.3006"]]},{"id":"98261154.3006","type":"http response","z":"553814a2.1248ec","name":"","statusCode":"","headers":{},"x":1730,"y":2420,"wires":[]},{"id":"34dc99e5.495466","type":"file in","z":"553814a2.1248ec","name":"","filename":"","format":"","chunk":false,"sendError":false,"encoding":"none","x":1550,"y":2380,"wires":[["98261154.3006"]]},{"id":"38d65d59.1d8aa2","type":"catch","z":"553814a2.1248ec","name":"","scope":null,"uncaught":false,"x":1200,"y":2440,"wires":[["3b8014a.86ad8ec","5b18a8e7.fb8da8"]]},{"id":"3b8014a.86ad8ec","type":"function","z":"553814a2.1248ec","name":"Set 404","func":"msg.payload = msg.error;\nmsg.statusCode = 404;//resource not found\nreturn msg;","outputs":1,"noerr":0,"x":1560,"y":2440,"wires":[["98261154.3006"]]},{"id":"5b18a8e7.fb8da8","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1350,"y":2480,"wires":[]},{"id":"a8c2985e.d23ad8","type":"ui_template","z":"553814a2.1248ec","group":"dfb4a60f.d788f8","name":"ui_temlplate - present download links on dashboard","order":0,"width":"12","height":"10","format":"<div ng-bind-html=\"msg.payload\"></div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":false,"templateScope":"local","className":"","x":1430,"y":2660,"wires":[[]]},{"id":"5de7cbb4.fa21a4","type":"comment","z":"553814a2.1248ec","name":"Create http endpoint <nodered>/files/xxx where xxx is the file name to download","info":"","x":1320,"y":2340,"wires":[]},{"id":"67ecfa7f.3f0e24","type":"http in","z":"553814a2.1248ec","name":"","url":"/files/:fn","method":"get","upload":false,"swaggerDoc":"","x":1110,"y":2380,"wires":[["f31a598d.9fd2c8"]]},{"id":"b724cbe904e0a0a4","type":"fs-ops-dir","z":"553814a2.1248ec","name":"","path":"basePath","pathType":"flow","filter":"*","filterType":"str","dir":"files","dirType":"msg","x":1260,"y":2600,"wires":[["889579700842c8c5"]]},{"id":"5bb555b2920a103f","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"c:\\temp\\","payloadType":"str","x":1120,"y":2260,"wires":[["26bc8255abc483dc"]]},{"id":"26bc8255abc483dc","type":"change","z":"553814a2.1248ec","name":"store flow.basePath","rules":[{"t":"set","p":"basePath","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1330,"y":2260,"wires":[[]]},{"id":"2184b260657996f6","type":"ui_ui_control","z":"553814a2.1248ec","name":"","events":"all","x":1100,"y":2600,"wires":[["b724cbe904e0a0a4"]]},{"id":"1b45551e6f9646f1","type":"comment","z":"553814a2.1248ec","name":"Setup basePath","info":"","x":1120,"y":2220,"wires":[]},{"id":"52034f887a419a0e","type":"comment","z":"553814a2.1248ec","name":"Get files in basePath and generate table","info":"","x":1190,"y":2560,"wires":[]},{"id":"d76416d92215b169","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1100,"y":2660,"wires":[["b724cbe904e0a0a4"]]},{"id":"889579700842c8c5","type":"template","z":"553814a2.1248ec","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n<table>\n {{#files}}\n <tr>\n <td> {{.}} </td>\n <td> <a href=\"/files/{{{.}}}\">download</a><td>\n </tr>\n {{/files}}\n</table>\n\n","output":"str","x":1440,"y":2600,"wires":[["a8c2985e.d23ad8"]]},{"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}]

```

* * *

#### PS: Uses `fs-ops` nodes for getting directly listing

---

_[View the full topic](https://discourse.nodered.org/t/unable-to-download-files-from-the-browser/51314)._
