Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write to file? #1

Open
ghost opened this issue Nov 23, 2020 · 5 comments
Open

Write to file? #1

ghost opened this issue Nov 23, 2020 · 5 comments

Comments

@ghost
Copy link

ghost commented Nov 23, 2020

Is it possible to write the upload file to disk? I tried with the file node in append mode, but it truncate the end of the file :(

@Alkarex
Copy link
Member

Alkarex commented Nov 23, 2020

Hello,
Yes it is possible to upload to disk. I will check the file node, but otherwise a simple function should do the job, following this convention https://nodered.org/docs/user-guide/messages#message-sequences

@Alkarex
Copy link
Member

Alkarex commented Nov 23, 2020

Try a flow like that (only for text/ascii data):

[
    {
        "id": "369592db.ff3c3e",
        "type": "ui_upload",
        "z": "2cb23187.5e68de",
        "group": "d8ea40a9.7243d8",
        "title": "upload",
        "name": "",
        "order": 0,
        "width": 0,
        "height": 5,
        "chunk": 256,
        "transfer": "binary",
        "x": 310,
        "y": 380,
        "wires": [
            [
                "3f03e074.34b688"
            ]
        ]
    },
    {
        "id": "3f03e074.34b688",
        "type": "file",
        "z": "2cb23187.5e68de",
        "name": "",
        "filename": "/tmp/test.txt",
        "appendNewline": false,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 470,
        "y": 380,
        "wires": [
            [
                "57939435.f397c4"
            ]
        ]
    },
    {
        "id": "57939435.f397c4",
        "type": "function",
        "z": "2cb23187.5e68de",
        "name": "Ready for next lines",
        "func": "return { tick: true };",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 680,
        "y": 380,
        "wires": [
            [
                "369592db.ff3c3e"
            ]
        ]
    },
    {
        "id": "d8ea40a9.7243d8",
        "type": "ui_group",
        "name": "Default",
        "tab": "909f2fda.b7e17",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "909f2fda.b7e17",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

image

I am considering doing files-to-chunks and chunks-to-file nodes that are better optimised and working with binary content as well.

@Alkarex
Copy link
Member

Alkarex commented Nov 23, 2020

Here is a better example with automatic filename:

[
    {
        "id": "369592db.ff3c3e",
        "type": "ui_upload",
        "z": "2cb23187.5e68de",
        "group": "d8ea40a9.7243d8",
        "title": "upload",
        "name": "",
        "order": 0,
        "width": 0,
        "height": 5,
        "chunk": 256,
        "transfer": "binary",
        "x": 310,
        "y": 380,
        "wires": [
            [
                "8131c214.ad0408"
            ]
        ]
    },
    {
        "id": "3f03e074.34b688",
        "type": "file",
        "z": "2cb23187.5e68de",
        "name": "",
        "filename": "",
        "appendNewline": false,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 610,
        "y": 380,
        "wires": [
            [
                "57939435.f397c4"
            ]
        ]
    },
    {
        "id": "57939435.f397c4",
        "type": "function",
        "z": "2cb23187.5e68de",
        "name": "Ready for next lines",
        "func": "return { tick: true };",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 800,
        "y": 380,
        "wires": [
            [
                "369592db.ff3c3e"
            ]
        ]
    },
    {
        "id": "8131c214.ad0408",
        "type": "function",
        "z": "2cb23187.5e68de",
        "name": "Filename",
        "func": "let filename = msg.file.name || 'tmp.txt';\nfilename = filename.replace(/[/\\\\]]/g, '_');\nmsg.filename = '/tmp/upload/' + msg.file.name;\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 460,
        "y": 380,
        "wires": [
            [
                "3f03e074.34b688"
            ]
        ]
    },
    {
        "id": "d8ea40a9.7243d8",
        "type": "ui_group",
        "name": "Default",
        "tab": "909f2fda.b7e17",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "909f2fda.b7e17",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

@sniicker
Copy link

Hi @Alkarex ,

i used your last flow to upload files and write them to disk.
It works with small files, but with larger files, i always miss some bytes and so the files are corrupt. >100mb
Can you help me please?

Nice greetings
sniicker

@Alkarex
Copy link
Member

Alkarex commented Apr 21, 2022

@sniicker Try to reduce a bit the chunk size in the parameters. Slower values should be more reliable but slower.
Feel free to share your flow so I can give it a try on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants