File download from Raspberry Pi local file

I installed node-red on my Raspberry Pi and saved the logs to pi's absolute path /home/pi/log_k.csv.
I want to download this file using the template node of the dashboard.
But when I tried to download, I get a "cannot GET error" message.

And my code is below.
node-red04

node-red03 node-red02

It's an urgent issue for me.T_T
Please kindly answer me. Thanks.

How are you saving the log? Notice who owns the file?

Your error says “Cannot GET”. You’re trying to access an absolute file system path over the web browser hoping it will magically work. Unless you set up a http-in and http-response node to host that file this won’t work.

On top of that it wouldn’t work for that reason too

Hi @aisejin

I am not getting/understanding exactly what you want to do, but there are a few things that are causing you grief.

As @afelix explained you need to set things up first to allow what you are trying to do to happen.

So, one way:
You will need to edit the settings.js file of node-red.

~/.node-red/settings.js

There is a httpStatic part which allows node-red to access files on the machine. Or something like that. I don't have the vocabulary to explain it much better.

But basically you set something up like:

httpStatic: '/home/pi/.node-red/public',

(DO NOT FORGET THE END COMMA!)

That then allows directories/files below there to be accessed.

So you will have to change where the file is saved to that path also.

Then you may get closer to doing what you want.

So say you put it in THAT directory. (/home/pi/.node-red/public/)

Then in your "template" the line would be:
<form action= "log_k.csv" method="download">

The path (/home/pi/.node-red/public/) is implied from the line in the settings.js file.

Does that help?

1 Like

Try adding a combination of http in (set to "get" and path something like /files/:filename) then hook that to a function node to construct a file path then connect that to a file in node to read the file then lastly to a http out node to serve the file to you.

Essentially, you create a http endpoint and check the msg.params, built a file path, load the file, serve the file.

Pretty sure there are examples here and also search this forum (been asked plenty of times)

Hi All,

I modified the code according to your comment. But I still got the error message.

@zenofmud @afelix
I installed node-red via root account. So ".node-red" folder is under the root directly. I so also moved the log file to "/root/.node-red/public". I also modified the permission of log(log_k.csv : chmod 777 log_k.csv).


So my code is as below.

[settings.js]
httpAdminRoot: '/admin',
httpStatic: '/root/.node-red/public',

[log]
node-red05


But I got error message "Cannot GET /ui/log_k.csv" now. What is the problem? What should I do? T_T.

node-red06

Thanks.

Hi @Steve-Mcl

Do you mean that I have to install plugin 'http'?
And I tried to access the examples according to your comment.
When I clicked ' Pretty sure there are examples here', I directed to 'https://flows.nodered.org/?num_pages=1' page. Is it the right operation?
I can't see the example pages.

Thanks.

I think you are opening a big can of worms doing that.

I would suggest you try editing the settings.js file and setting the httpStatic as I mentioned, with the path I suggested.
Of course you will have to make a directory called "public" in your .node-red directory.

Oh, do those things the other way around: Make the directory THEN edit the settings.js file.

Then. . .

Put a file in that directory. For the sake of the exercise make it a picture.
Start Node-Red.
Open a browser and enter this:
< your ip address >:1880/(name of picture)
See if it shows the picture.

If it does you have confirmed that you can access that directory (and any below it)

With that "structure" (< your IP address >:1880/. . . ) put that in your GET thingy.

Not the best solution, but the only one I know.

@Trying_to_learn

I have solved this issue!
It didn't matter that the user is root.


[root/.node-red/setting.js]
httpAdminRoot: '/admin',
httpStatic: '/root/.node-red/public',
ui: { path: "/" },


The before setting.js is 'ui: { path : "ui" }, ',
so I changed the settings.js to ui: { path: "/" }, from ui: { path : "ui" },.
After that, I succeed the download file from a web server.
I really appreciate all your help.
Thanks.

I’m happy it works for you that way, but internally I’m crying seeing it all run as root. Like @Trying_to_learn said you’re taking a huge risk with this. It’s almost always a bad idea to run node-red as root. You mentioned in the first post your question was urgent, but I hope you can find time to set it up differently and not run as root instead. Crudely said, this is waiting for problems to happen, and giving a programming language (JavaScript) inside Node-RED that also has file access and web pages access to your machine is waiting for it to go wrong. I truly hope your pi is not having node-red accessible remotely.

No, I meant the built in http node.

No need to modify settings.js & using standard built in nodes.

See this flow...
https://flows.nodered.org/flow/db68bd4934cf46f39e6e453a348bc419