Download a file using a button - connection lost


#1

Hello together,
I would like to download a csv-file via the nodered dashboard.
I found verry usefull posts in this forum and now it works! But if I hit the download button, the dashboard looses connection and the web page is loaded again. Is this normal for this appilcation or is there a problem?
Another Question is, i would like to use a normal dashborad button and not an html-template-button. Is this possible? if yes, how can i do this?

thank you and best regards
Florian


#2

Sharing your flow will be helpful. I just have a few questions:
Is the browser running on the same machine as NR.
What versions of node-red and node.js?
Which file node are you using?
How big is the file?
Do you see anything on the node-red log?


#3

Hello zenofmud,

here my answers to your quesions:

  • No, my browser is not running on the same machine. My nodered runs on a beagleboneblack (1Ghz) and my browser on a labtop via Cable-Ethernet.
  • The node.js version is the v4.8.7
  • My nodered version is the v0.17.5
  • The file node is a "file in"
  • The file has 17.9 KB
  • What do you mean with the "node-red log"? In the debug-window is no entry.

My flow is:
[{"id":"8a4440e7.4e8e7","type":"http in","z":"69794c24.bda4cc","name":"","url":"/HumiData.csv","method":"get","upload":false,"swaggerDoc":"","x":110,"y":720,"wires":[["4fa2d392.0122bc"]]},{"id":"4fa2d392.0122bc","type":"file in","z":"69794c24.bda4cc","name":"","filename":"/home/debian/HumiData.csv","format":"","sendError":true,"x":360,"y":720,"wires":[["a508bb21.b41978"]]},{"id":"d659f021.5555f8","type":"http response","z":"69794c24.bda4cc","name":"","statusCode":"","headers":{},"x":590,"y":780,"wires":[]},{"id":"a508bb21.b41978","type":"change","z":"69794c24.bda4cc","name":"","rules":[{"t":"set","p":"headers['Content-Disposition']","pt":"msg","to":"attachment; filename=HumiData.csv","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":210,"y":780,"wires":[["d659f021.5555f8"]]},{"id":"66cec20f.09a8e4","type":"ui_template","z":"69794c24.bda4cc","group":"e7c0de75.6d64b","name":"Download HumiData.csv","order":0,"width":0,"height":0,"format":"<form action="/HumiData.csv" method="get">\n <input type="submit" value="Download LogFile" \n name="Submit" id="frm1_submit" />\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":130,"y":660,"wires":[[]]},{"id":"e7c0de75.6d64b","type":"ui_group","z":"","name":"Entfeuchter Chart","tab":"52e9e30f.d04acc","order":2,"disp":true,"width":"6","collapse":false},{"id":"52e9e30f.d04acc","type":"ui_tab","z":"","name":"Entfeuchter","icon":"dashboard","order":4}]


#4

A couple things.

  1. NR version is every old - current version is v0.19.3
  2. node.js is very old and is EOL - current version you should use is v8.x
  3. Your flow will not import, but I hacked it to import it BUT it looks like the template does not have a . Can you open your template, copy the code and insert it in a response - make sure to enclose it with three backtic's

#5

Just tried it with no problems (with template fixed with the </form> added)
Running on a Pi 2 Model B
NR: v0.18.7
node.js: v8.11.2
browser: Safari in OSX 10.13.6
CSV File size: 1M
local network and it flys with no problem.


#6

hello zenofmud,
thank you for your help and testing. Maybe it is a problem of the older versions. At this time i don't want to change anything on my running system :slight_smile:
Maybe if i have more experience with the NR and the linux system i will update NR and node.js. So I will close this thread.

best regards
Florian


#7

[edit] double post.


#8

did you add in the </form>?


#9

I am trying to do something very similar, but I can not use the code from above.

Currently, I am creating the csv, emailing, and then deleting the csv file. I want to create the csv and then download it with one button click.

[{"id":"2edba964.cb42d6","type":"function","z":"8ed48002.ec4c3","name":"SQL Function","func":"stDt = msg.payload.startDate;\nedDt = msg.payload.endDate;\n\npld = \"SELECT * \";\npld = pld + \"FROM MachineData \";\npld = pld + \"WHERE Timestamp BETWEEN \";\npld = pld + \"'\"+stDt+\"' and '\"+edDt+\"' \" ;\npld = pld + \"order by timestamp \";\npld = pld;\n\nmsg.payload = pld;\nreturn msg;\n\n\n\n\n","outputs":1,"noerr":0,"x":380,"y":580,"wires":[["1538e493.7c6e6b"]]},{"id":"1538e493.7c6e6b","type":"MSSQL","z":"8ed48002.ec4c3","mssqlCN":"2f6e0812.05a8b8","name":"MSSQL","query":"","outField":"payload","x":540,"y":580,"wires":[["c9291aaf.7a0d28"]]},{"id":"c9291aaf.7a0d28","type":"change","z":"8ed48002.ec4c3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t payload[0].$join($keys(), \",\"),\t payload[].$join(*.$string(), \",\")\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":580,"wires":[["66608aa9.6f6924"]]},{"id":"66608aa9.6f6924","type":"split","z":"8ed48002.ec4c3","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":850,"y":580,"wires":[["76682d71.e10764"]]},{"id":"76682d71.e10764","type":"file","z":"8ed48002.ec4c3","name":"","filename":"/home/pi/RawMachineData.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1070,"y":580,"wires":[[]]},{"id":"6d367abe.66cf54","type":"function","z":"8ed48002.ec4c3","name":"","func":"msg.to = msg.payload.to;\nmsg.payload = '';\nmsg.topic = 'RawMachineData Export'\nmsg.attachments = [{\n filename: 'RawMachineData.csv',\n path: '/home/pi/RawMachineData.csv'\n}];\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":620,"wires":[["be7cb96b.1321a8","8b686191.d45d6"]]},{"id":"be7cb96b.1321a8","type":"e-mail","z":"8ed48002.ec4c3","server":"smtp.gmail.com","port":"465","secure":true,"name":"","dname":"","x":850,"y":620,"wires":[]},{"id":"8b686191.d45d6","type":"delay","z":"8ed48002.ec4c3","name":"","pauseType":"delay","timeout":"20","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":860,"y":660,"wires":[["434708fc.962f58"]]},{"id":"434708fc.962f58","type":"exec","z":"8ed48002.ec4c3","command":"rm /home/pi/RawMachineData.csv","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1160,"y":660,"wires":[[],[],[]]},{"id":"430207d4.5458c8","type":"json","z":"8ed48002.ec4c3","name":"","property":"payload","action":"","pretty":false,"x":230,"y":580,"wires":[["2edba964.cb42d6","9ef66eca.a1363"]]},{"id":"8877fa9d.8a4f88","type":"mqtt in","z":"8ed48002.ec4c3","name":"","topic":"LH35N3000B","qos":"2","broker":"302d9396.8bf4ec","x":90,"y":580,"wires":[["430207d4.5458c8"]]},{"id":"9ef66eca.a1363","type":"delay","z":"8ed48002.ec4c3","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":360,"y":620,"wires":[["6d367abe.66cf54"]]},{"id":"2f6e0812.05a8b8","type":"MSSQL-CN","z":"","name":"NewDev","server":"10.0.0.11","encyption":true,"database":"Dev"},{"id":"302d9396.8bf4ec","type":"mqtt-broker","z":"","name":"","broker":"10.0.0.2","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

Thanks,
Mike


#10

@arneym Mike
What version of NR and node?
What platform/OS?

I’m out but will take a look when I get back in a couple hours


#11

I am using version 0.19.4 using Raspian.
I would like to download on any device though.

Pic of flow. Code is in my first post.

Thanks @zenofmud !


#12

I can make a hyperlink in a template node work, but I need to give a start date and end date parameter. I would like to use a button instead of a hyperlink.

I have a form on a Windows instance of NR. It sends the data via MQTT to a Raspberry Pi instance of NR. Main goal is to download the file from the Raspberry Pi and then delete it from the Pi after download.


#13

Ok, questions, why are you splitting the delete flow from the creation flow? It would seem that you would want to create the csv file then go right to the email send followed by the remove of the file.


#14

The msg.to is pulled from the JSON node. I tried to send email after creation, but it didn't pick up msg.to.

I do not want to email the file anymore. I would like to download the file instead of emailing.

I had to have the Raspberry Pi handle the file creation, email, and deletion because I could not get Windows file deletion to work.

PC Flow:
[{"id":"63de8626.d1f168","type":"comment","z":"60f57708.6d6238","name":"Raw LH35N-3000B Data Export","info":"","x":250,"y":280,"wires":[]},{"id":"7935123f.af3b8c","type":"ui_form","z":"60f57708.6d6238","name":"","label":"Raw Machine Data Export","group":"db835013.4ce0a","order":0,"width":0,"height":0,"options":[{"label":"Email","value":"to","type":"email","required":true},{"label":"StartDate","value":"startDate","type":"text","required":true},{"label":"EndDate","value":"endDate","type":"text","required":true}],"formValue":{"to":"","startDate":"","endDate":""},"payload":"","submit":"Submit","cancel":"Cancel","topic":"","x":230,"y":320,"wires":[["65813b94.777f74"]]},{"id":"65813b94.777f74","type":"mqtt out","z":"60f57708.6d6238","name":"","topic":"LH35N3000B","qos":"2","retain":"","broker":"200dfb1c.eed1c4","x":460,"y":320,"wires":[]},{"id":"db835013.4ce0a","type":"ui_group","z":"","name":"Machine Data","tab":"7b05b022.b7aa8","disp":true,"width":"6","collapse":false},{"id":"200dfb1c.eed1c4","type":"mqtt-broker","z":"","name":"xBee","broker":"10.0.0.2","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"7b05b022.b7aa8","type":"ui_tab","z":"","name":"Reports","icon":"dashboard","order":37}]

image

Raspberry Pi Flow:

[{"id":"d20b2bd4.2e4158","type":"comment","z":"cbfed81f.36c5d8","name":"Raw LH35N-3000B Data Export","info":"","x":170,"y":200,"wires":[]},{"id":"1f54e700.77d3b9","type":"function","z":"cbfed81f.36c5d8","name":"SQL Function","func":"stDt = msg.payload.startDate;\nedDt = msg.payload.endDate;\n\npld = \"SELECT * \";\npld = pld + \"FROM MachineData \";\npld = pld + \"WHERE Timestamp BETWEEN \";\npld = pld + \"'\"+stDt+\"' and '\"+edDt+\"' \" ;\npld = pld + \"order by timestamp \";\npld = pld;\n\nmsg.payload = pld;\nreturn msg;\n\n\n\n\n","outputs":1,"noerr":0,"x":400,"y":240,"wires":[["30599455.d9ebdc"]]},{"id":"30599455.d9ebdc","type":"MSSQL","z":"cbfed81f.36c5d8","mssqlCN":"52a98399.71474c","name":"MSSQL","query":"","outField":"payload","x":560,"y":240,"wires":[["755c43d6.c391cc"]]},{"id":"755c43d6.c391cc","type":"change","z":"cbfed81f.36c5d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t payload[0].$join($keys(), \",\"),\t payload[].$join(*.$string(), \",\")\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":240,"wires":[["4f0c8210.eaa76c"]]},{"id":"4f0c8210.eaa76c","type":"split","z":"cbfed81f.36c5d8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":870,"y":240,"wires":[["634bb8b3.650128"]]},{"id":"634bb8b3.650128","type":"file","z":"cbfed81f.36c5d8","name":"","filename":"/home/pi/RawMachineData.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1090,"y":240,"wires":[[]]},{"id":"6d94ccdc.242cb4","type":"function","z":"cbfed81f.36c5d8","name":"","func":"msg.to = msg.payload.to;\nmsg.payload = '';\nmsg.topic = 'RawMachineData Export'\nmsg.attachments = [{\n filename: 'RawMachineData.csv',\n path: '/home/pi/RawMachineData.csv'\n}];\nreturn msg;","outputs":1,"noerr":0,"x":690,"y":280,"wires":[["1386ec1d.3a9e64","11b2bcfe.636f33"]]},{"id":"1386ec1d.3a9e64","type":"e-mail","z":"cbfed81f.36c5d8","server":"smtp.gmail.com","port":"465","secure":true,"name":"","dname":"","x":870,"y":280,"wires":[]},{"id":"11b2bcfe.636f33","type":"delay","z":"cbfed81f.36c5d8","name":"","pauseType":"delay","timeout":"20","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":880,"y":320,"wires":[["51c484ab.9df8fc"]]},{"id":"51c484ab.9df8fc","type":"exec","z":"cbfed81f.36c5d8","command":"rm /home/pi/RawMachineData.csv","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1180,"y":320,"wires":[[],[],[]]},{"id":"343da1d8.fda39e","type":"json","z":"cbfed81f.36c5d8","name":"","property":"payload","action":"","pretty":false,"x":250,"y":240,"wires":[["1f54e700.77d3b9","cf4cd255.b7f46"]]},{"id":"cd9fb7d3.4debd8","type":"mqtt in","z":"cbfed81f.36c5d8","name":"","topic":"LH35N3000B","qos":"2","broker":"30af8e0d.e09262","x":110,"y":240,"wires":[["343da1d8.fda39e"]]},{"id":"cf4cd255.b7f46","type":"delay","z":"cbfed81f.36c5d8","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":380,"y":280,"wires":[["6d94ccdc.242cb4"]]},{"id":"52a98399.71474c","type":"MSSQL-CN","z":"","name":"NewDev","server":"10.0.0.11","encyption":true,"database":"Dev"},{"id":"30af8e0d.e09262","type":"mqtt-broker","z":"","name":"","broker":"10.0.0.2","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]


#15

Ok, first back up your flow.

Disconnect the wire fromn the json node to the delay 1m node.

Next, in the first function (SQL Function) add as the first line flow.set("msgto", msg.payload.to); which will put the 'to' in a flow context variable.

Tie the 'file' node to the second function node and replace the line
msg.to = msg.payload.to; with
msg.to = flow.get("msgto");

Then copy the 'file' node and attach it to the output of the 'delay 20' node and change the option to delete the file. This way, there is no need to use the exec node.


#16

Ahh I just remembered you want to download the file.

So, in a browser, you want to

  1. enter a start and end date
  2. press a button
  3. read the dates,
  4. go out to a database and retreave data baased on the dates
  5. create a csv file
  6. download the file to the users computer
  7. and finally delete the file from the machine running NR.

oh and somewhere in there send an email message.

I think you need to look at one of the FTP nodes for the download part of this, you have most of the rest already setup. If you you my prevous suggestion, the FTP node should go before the 'delete the file' node.


#17

Thanks @zenofmud! I will give it a shot. I want to do 1-7. No need to email the csv if I can download it.


#18

I will be eliminating this report, since I got the approval to put my NR machine on my corporate network.

Thanks for looking though.