How to download a file using dashboard button

5,986 views
Skip to first unread message

Thomas Schäfer

unread,
Mar 4, 2017, 5:16:49 PM3/4/17
to Node-RED
Hi all!
I'm pretty new to node-red and I found no solution for my problem.
I receive some sensor data. I can save the data in csv format to a file. So far, so good. Now I want to download the file using a dashboard button, but I found no "download node". How can I do this?

Best regards,
Thomas

Colin Law

unread,
Mar 4, 2017, 5:20:38 PM3/4/17
to node...@googlegroups.com
What do you mean by 'download'? Download from where to where?

Colin

On 4 March 2017 at 22:16, 'Thomas Schäfer' via Node-RED
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
> To post to this group, send an email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/06245138-b4c9-4742-b74e-6752fb52ee96%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Thomas Schäfer

unread,
Mar 6, 2017, 1:15:29 AM3/6/17
to Node-RED
I like to download the file from the Node-Red machine to the remote computer.

Best regards,
Thomas

Thomas Schäfer

unread,
Mar 8, 2017, 1:05:28 PM3/8/17
to Node-RED
Is there really no solution?

Best regards,
Thomas

steve rickus

unread,
Mar 8, 2017, 3:33:33 PM3/8/17
to Node-RED
I think the solution will be to define an http-in node (e.g. /download.csv), linked to a file-read node (to get the file content), linked to a change node (to set the msg headers/payload), linked to an http-response node (to return the file contents)

The requestor (whoever is pushing the button) will probably have to be redirected to the /download.csv url, in order to kick off the above flow. You don't really need a ui-button node for that -- just an html button with an onclick js function call, or any of a dozen other ways using html/js.

I've not tried to set the msg header/payload/body/whatever, so you will have to look up some docs/examples to find the correct settings. If you do get it working, please post the solution back here, if you are able to.

Nick O'Leary

unread,
Mar 8, 2017, 3:46:18 PM3/8/17
to Node-RED Mailing List
Hi,

as Steve says, the basic principle is to use an HTTP triggered flow that can respond to a request from the browser to download the file.

The trick you need to know to get the browser to download the file rather than attempt to display it is to set the 'Content-Disposition' header in the http response to: "attachment; filename=foo.zip"

For example, the following change node does just that:
[{"id":"3572d02e.46ab3","type":"change","z":"feee1df.c3263e","name":"","rules":[{"t":"set","p":"headers['Content-Disposition']","pt":"msg","to":"attachment; filename=fred.zip","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":680,"wires":[["ea8d8a4c.2c8388"]]}]

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.

Xavier Vilajosana

unread,
Aug 7, 2017, 11:31:30 PM8/7/17
to Node-RED

Hi,

I tried the following but I could not make it work. Any advise is welcome.

Basically, the button connects to an http request to /file. Then in a separate flow I handle the http Request, loading the file from /tmp/file.txt, adding the header modification as pointed out by Nick and connecting to an http response. 

From the UI, when I press the button I see the debug output indicating that the text is being responded to the button request (which is good) but it does not download as a file in the browser. 

here the example I use.


[{"id":"859d8eb8.2e697","type":"http in","z":"b21af887.3d1a68","name":"","url":"/file","method":"get","swaggerDoc":"","x":153.5,"y":359,"wires":[["807a3871.39fce8"]]},{"id":"92dcd128.09a82","type":"ui_button","z":"b21af887.3d1a68","name":"","group":"9c59a9e0.ff3df8","order":0,"width":0,"height":0,"label":"button","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":146.5,"y":196,"wires":[["138b823b.350cde"]]},{"id":"138b823b.350cde","type":"http request","z":"b21af887.3d1a68","name":"","method":"GET","ret":"txt","url":"http://127.0.0.1:1880/file","tls":"","x":354.5,"y":193,"wires":[["1a49ab33.104335"]]},{"id":"9e242ac4.19bea8","type":"http response","z":"b21af887.3d1a68","name":"","x":783.5,"y":465,"wires":[]},{"id":"807a3871.39fce8","type":"file in","z":"b21af887.3d1a68","name":"","filename":"/tmp/file.txt","format":"utf8","x":354.5,"y":369,"wires":[["24d29822.ae0088"]]},{"id":"24d29822.ae0088","type":"change","z":"b21af887.3d1a68","name":"","rules":[{"t":"set","p":"headers['Content-Disposition']","pt":"msg","to":"attachment; filename=file.zip","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":505,"y":511,"wires":[["9e242ac4.19bea8","6011a6f9.c2c768"]]},{"id":"1a49ab33.104335","type":"debug","z":"b21af887.3d1a68","name":"","active":true,"console":"false","complete":"false","x":592.5,"y":190,"wires":[]},{"id":"6011a6f9.c2c768","type":"debug","z":"b21af887.3d1a68","name":"","active":true,"console":"false","complete":"false","x":744.5,"y":385,"wires":[]},{"id":"9c59a9e0.ff3df8","type":"ui_group","z":"","name":"main","tab":"12557942.452e77","disp":true,"width":"8"},{"id":"12557942.452e77","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":"1"}]

steve rickus

unread,
Aug 8, 2017, 12:13:06 PM8/8/17
to Node-RED
A bit more information about how this is failing would be helpful -- can you see any response code/headers/body in the browser's Dev Console (F12)? Are the file contents showing up in the browser instead of being downloaded, or is there an error page?

I did notice that you are reading a text file, returning a UTF-8 string, and setting the attachment's filename to "file.zip". I don't know if that combination will work without setting some other content-type headers.

Also, I'm wondering if the msg.res field is being returned ok from the file read node (probably, but i would send its output to a debug node showing the "complere msg object" just to make sure). Finally, you may be able to eliminate the change node and add the msg.headers directly in the settings for http-response node? Hope some of this helps...
--
Steve

ivan kostov

unread,
Oct 6, 2017, 11:26:43 AM10/6/17
to Node-RED
Hi Xavier,

after you fix the wrong filename you will be able to download your file by pointing the browser to http://localhost/file. However I still don't know how to do the forwarding. I mean - when someone clicks the button, forward the request to http://localhost:1880/file and return the content.

Cheers,
Ivan

[{"id":"d8d2e890.9126e8","type":"http in","z":"894a7820.4db228","name":"","url":"/file","method":"get","upload":false,"swaggerDoc":"","x":129,"y":701,"wires":[["890f4965.8663e8"]]},{"id":"1cee4cc9.8a3a13","type":"http response","z":"894a7820.4db228","name":"","x":886,"y":834,"wires":[]},{"id":"890f4965.8663e8","type":"file in","z":"894a7820.4db228","name":"","filename":"/tmp/file.txt","format":"","sendError":true,"x":306,"y":881,"wires":[["9fd11570.93e658"]]},{"id":"9fd11570.93e658","type":"change","z":"894a7820.4db228","name":"","rules":[{"t":"set","p":"headers['Content-Disposition']","pt":"msg","to":"attachment; filename=file.txt","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":607.5,"y":880,"wires":[["1cee4cc9.8a3a13"]]}]

Matías Lagos

unread,
Nov 24, 2017, 2:07:56 AM11/24/17
to Node-RED
Hello! to all, I found a momentary solution to the problem.
Generate a button that allows us to download the file by means of a Template
I attach the code


[{"id":"f522e00e.b77828","type":"ui_template","z":"9ca621eb.96c888","group":"744fc844.d92de","name":"","order":0,"width":0,"height":0,"format":"<form action=\"/archivo\" method=\"get\">\n    <input type=\"submit\" value=\"Descargar Registros\" \n         name=\"Submit\" id=\"frm1_submit\" />\n</form>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":660,"y":360,"wires":[[]]},{"id":"744fc844.d92de","type":"ui_group","z":"","name":"Registros","tab":"d74652a9.4a33d8","disp":true,"width":"6"},{"id":"d74652a9.4a33d8","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

Regards!

Diego Quiroz

unread,
May 31, 2018, 9:19:58 PM5/31/18
to Node-RED
Hello everyone,

Use the flow that Matias Lagos shared in the last comment (modifying the path "/ archivo" with "/home/pi/Public/consulta/TablaC.txt").

Pressing the button returns "Can not GET /home/pi/Public/consulta/TablaC.txt"


Code in node "template":
---------------------------------------------------------------------------------------
<form action="/home/pi/Public/consulta/TablaC.txt" method="get">
    <input type="submit" value="Descargar Registros" 
         name="Submit" id="frm1_submit" />
</form>
-----------------------------------------------------------------------------------------

Does anyone know why or could you help me?

Thank you very much!

Diego

steve rickus

unread,
Jun 1, 2018, 9:19:28 AM6/1/18
to Node-RED
Look at the URL that is being used by the browser -- "192.168.0.100:1880/home/pi/Public/consulta/TablaC.txt?Submit=Descargar+Registros"

Do you have the httpStatic directory defined in your settings.js file? If so, it will show up in the node-red logs when the it starts up... what directory is that using?
If you have /home/pi/Public (for instance), you would exclude that from your URL -- so your GET path would be just "/consulta/TablaC.txt". You can test this in your browser to just entering the URL "192.168.0.100:1880/consulta/TablaC.txt" and see if anything gets returned.

However, this is a very unusual way to try to download a file... by using a static file url as the action on a form. Why would you choose that? The form submit action will try to append the form fields/values to the url (thus the ?Submit=... stuff), which would be ignored by the static file response.
--
Steve

P.S. Since this forum is closing down, please post any new topics on the discourse site.

Diego Quiroz

unread,
Jun 1, 2018, 1:20:56 PM6/1/18
to node...@googlegroups.com
Thank you very much for the quick answer.

I have solved it, as Steve Rickus said, I already had the httpStatic in "/home/pi/Public/consulta" so the GET route should be "/TablaC.txt" nothing more.

Using the code in the "template" node:
-------------------------------------------------- -------------------------------------
<form action = "/ TablaC.txt" method = "get">
    <input type = "submit" value = "Download Records"
         name = "Submit" id = "frm1_submit" />
</ form>
-------------------------------------------------- -------------------------------------
The contents of the TablaC.txt file is displayed in the browser but it is not downloaded.


Modify it by
-------------------------------------------------- -----------------------------------------
    <a href="/TablaC.txt" download="TablaC.txt"> Download Text </a>
-------------------------------------------------- -----------------------------------------
and now if pressing the button the file is downloaded without problems.
 

Matias: 
Cambiando la ruta a "/ var / www" o "/ var / www / html" con el mismo código me seguía pasando lo mismo (aparecía el mensaje "Cannot GET /home/pi/Public/consulta/ TablaC.txt") 


Many thanks to both!
regards

Diego

--
http://nodered.org
 
** We're moving this mailing list over to the new Node-RED Forum: https://discourse.nodered.org **

 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages