# Add files to workitem in NodeRED based on form

**URL:** <https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921>\
**Category:** OpenCore\
**Created:** [November 18, 2024, 5:18pm UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921 "2024-11-18T17:18:52Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 18, 2024, 5:18pm UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/1 "2024-11-18T17:18:52Z")

</div>

Hi guys,  
I was not able to find a guide on this so i will just create a new topic

I have a form in OpenFlow which looks like this:

 ![msedge_3Gkd5tvXac](https://discourse.openiap.io/uploads/default/original/2X/5/576e52211d0a28b767d696f2d3dd9ce0a55309dd.png)

which is connected to som nodes in NodeRED:

 ![msedge_f7oeXNRtKd](https://discourse.openiap.io/uploads/default/original/2X/1/14ba3cab426d30b4a443d9b0ff3a025ca177e759.png)

Now when i activate the workflow and submit a file in the form i can see the api property **file** which is an array. I assume this array can be attatched directly to the Workitem if assigned to the msg.files property.  
function node _Prepare WI_:

```auto
msg.files = msg.payload.container.file

delete msg.payload

return msg

```

In my case the file component is within a container as seen here:  
 ![msedge_XsRiD96s2c](https://discourse.openiap.io/uploads/default/original/2X/e/e9ac30f5f7b04034a15dc3d103b17aa27ff3c9b2.png)

but when the workitem is created the file isnt attatched to it:

 ![msedge_Fr82d2EAVs](https://discourse.openiap.io/uploads/default/original/2X/5/5a32d26008cdc11d5ad418d7f9b5c91371162cc9.png)

what is the right way to achieve this?

i also thought about query the openflow database files collection but i dont see an \_id property of the file in the debug message

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 19, 2024, 4:33pm UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/2 "2024-11-19T16:33:37Z")

</div>

I have also tried to just use a get request using the _http request_ node but i get this error:  
 ![image](https://discourse.openiap.io/uploads/default/original/2X/b/b3fa6da07dade13bc92ef9a6d36303a955d90def.png)  
i havent found a way to make reuqests on NodeRED that allows to ignore ssl errors

I dont want to change the NodeRED env variables like this:  
NODE\_TLS\_REJECT\_UNAUTHORIZED=0

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 19, 2024, 5:53pm UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/3 "2024-11-19T17:53:55Z")

</div>

Okay, so the http request node has a property that you can set to get around this:  
 ![image](https://discourse.openiap.io/uploads/default/original/2X/6/69929f14e1a66da9e0238d8e984e60234056653f.png)  
but now i am confused how the request returns status 404 when i just provide the url of the link to the download.

if i paste the url directly into the browser i download the file without issues. but i get status 404 if i run the request from nodered or postman

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 19, 2024, 7:11pm UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/4 "2024-11-19T19:11:24Z")

</div>

After inspecting the URL response, I noticed multiple headers that are presumably designed to ensure browser-like interaction for the request. Please correct me if I’m wrong, but this suggests that the URL is not intended to be used for retrieving the file content programmatically.

---

<div class="post-metadata">

**Author:** ![Rutger\_van\_Schoonhov](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@Rutger\_van\_Schoonhov](https://discourse.openiap.io/u/Rutger_van_Schoonhov)\
**Post date:** [November 19, 2024, 8:54pm UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/5 "2024-11-19T20:54:32Z")

</div>

> [@Peter\_Madsen](#):
>
> `msg`

Hi,  
I have created an example for adding files to workitems in NodeRED.  
Main thing: msg.files should contain a list of files.  
The file itself should be a bufffer.

```auto
[
    {
        "id": "f7b0b5448c53a937",
        "type": "function",
        "z": "f2c1449a89fa18cc",
        "name": "create wi with file",
        "func": "// We want someStruct as a json file attached to a workitem\nconst someStruct = {\n \"name\": \"example\",\n \"greeting\": \"Hello world!\"\n};\n\n// I grabbed it from an example where I wanted something downloaded by an agent. \nvar downloadFile = {\n \"filename\": \"test.json\",\n file: Buffer.from(JSON.stringify(someStruct))\n}\nmsg.files = [downloadFile];\n\nmsg.newWorkitem = {\n \"name\": \"test\",\n \"payload\": {\n \"key1\":\"Put your payload here\" \n }\n}\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1750,
        "y": 360,
        "wires": [
            [
                "096a5029d1f9c0c8"
            ]
        ]
    },
    {
        "id": "096a5029d1f9c0c8",
        "type": "workitem addworkitem",
        "z": "f2c1449a89fa18cc",
        "name": "",
        "config": "3ada51595225d3e1",
        "payload": "newWorkitem.payload",
        "payloadtype": "msg",
        "workitem": "workitem",
        "workitemtype": "msg",
        "topic": "newWorkitem.name",
        "topictype": "msg",
        "nextrun": "nextrun",
        "nextruntype": "msg",
        "priority": "2",
        "prioritytype": "num",
        "files": "files",
        "filestype": "msg",
        "success_wiq": "success_wiq",
        "success_wiqtype": "msg",
        "failed_wiq": "failed_wiq",
        "failed_wiqtype": "msg",
        "x": 1930,
        "y": 360,
        "wires": [
            [
                "391caa8edfa16ee7"
            ]
        ]
    },
    {
        "id": "c827806d3f8dd9b0",
        "type": "inject",
        "z": "f2c1449a89fa18cc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1580,
        "y": 360,
        "wires": [
            [
                "f7b0b5448c53a937"
            ]
        ]
    },
    {
        "id": "391caa8edfa16ee7",
        "type": "debug",
        "z": "f2c1449a89fa18cc",
        "name": "test file",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 2080,
        "y": 360,
        "wires": []
    },
    {
        "id": "3ada51595225d3e1",
        "type": "iworkitemqueue-config",
        "wiq": "testq",
        "wiqid": "",
        "name": "testq"
    }
]

```

Hope it helps.  
Gr,  
Rutger  
PS: You can select and copy the code, then press CTRL - I (import) in NodeRED, paste, click Import and deploy.

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 20, 2024, 9:55am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/6 "2024-11-20T09:55:01Z")

</div>

Thank you, for sharing your solution

Maybe i am missing something, but i dont see how i can use this to add the files directly chosen in the openflow form. Will this work with that too?

---

<div class="post-metadata">

**Author:** ![Rutger\_van\_Schoonhov](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@Rutger\_van\_Schoonhov](https://discourse.openiap.io/u/Rutger_van_Schoonhov)\
**Post date:** [November 20, 2024, 10:28am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/7 "2024-11-20T10:28:46Z")

</div>

I am sorry, I was too quick. I thought you had the data already in NodeRED.  
I need to dive into that with the form. (it has been years since I last used forms, but I should have an example somewhere).  
I’ll take a look, but it may take me some time as deadlines at work are approaching rappidly…

---

<div class="post-metadata">

**Author:** ![AndrewK](https://discourse.openiap.io/user_avatar/discourse.openiap.io/andrewk/32/1476_2.png) [@AndrewK](https://discourse.openiap.io/u/AndrewK)\
**Post date:** [November 20, 2024, 10:54am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/8 "2024-11-20T10:54:18Z")

</div>

I was curious about this one as well, and tried a small workflow, and I think I’m also missing something.

What I got working, although it’s _clearly_ a workaround, was:  
Upload the file through the form.  
Get the `name` property out of the payload (the one with the guid in it, not the original name), query the files collection for the id of the object with that uniqueName, and add that to the workitem. After that, in OpenRPA/agent, fetch it using the id and GetFile/download.

What I tried that didn’t work:

- Use `download` node in NodeRed to get the file as a buffer to pass it to workitem → access denied (why? I don’t know - it seems illogical, as all the ACL seem fine)
- Use GetFile in OpenRPA with UniqueName as the name → file not found (it can only download by the non-unique filename, and that is not what’s needed in most cases)

I’m pretty sure I’m missing something, but that feels like a very roundabout way of doing it, and I’m pretty sure there should be a shorter path to this that I don’t know.

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 20, 2024, 11:33am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/9 "2024-11-20T11:33:07Z")

</div>

No worries i appreciate the thought. GL at work! 🙂

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 20, 2024, 11:41am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/10 "2024-11-20T11:41:30Z")

</div>

Exactly my thoughts as well. It seems impossible to use the URL directly from the file component as there are so many headers required on the download link.

That was also the only workaround i could get to work, but it does seem a bit of a detour as it feels like the informaiton should be right there, easy to access and pass, at least having the file \_id property be available.

I’m currently working on a solution to make this proces a bit more stream-lined

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 21, 2024, 7:54pm UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/11 "2024-11-21T19:54:44Z")

</div>

I came up with a subflow to make things easier.  
If anyone has a different solutions im still very curious to see that.

If you want to check out the subflow you can find it here:  
[Get files from OpenFlow - NodRED - Guides - OpenIAP](https://discourse.openiap.io/t/get-files-from-openflow-nodred/1927)

---

<div class="post-metadata">

**Author:** ![AndrewK](https://discourse.openiap.io/user_avatar/discourse.openiap.io/andrewk/32/1476_2.png) [@AndrewK](https://discourse.openiap.io/u/AndrewK)\
**Post date:** [November 22, 2024, 9:46am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/12 "2024-11-22T09:46:21Z")

</div>

Thanks Peter, the subflow looks nicely done (also learned a thing or two about designing more readable subflows thanks to this 😉 ).

Question though - what was the reason for the static delay at the start?

One thing to note, is that using this method to attach the file to wi seems to be duplicating the file - the form uploads it, then the wi submission uploads it again from the buffer, so that needs to be potentially cleaned out in the calling workflow (f.e. deleting the original file after adding the wi, to avoid clutter).

That said, all of this would be much easier if the upload form would be adding the file `_id` to its payload (which could be used in download file directly as a unique identifier).

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 22, 2024, 10:09am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/13 "2024-11-22T10:09:47Z")

</div>

you are welcome, im glad to contribute to this awesome forum.

Im glad you mention the bit about adding the file to the workflow it took me a while to figure out where this extra file came from i didnt decide on removing the extra file but yes it is not convinent to have a dupe.

but in my opinion i think it would make a lot more sense to have a build in property on the add workitem node like (attach files from openflow using uniqueue names) so that the dupes could bed avoided

also the delay is just to quickly reflect the nodes status when activated. hope it makes sense 🙂

indeed totally agree with the \_id that was what i attempted to begin with

---

<div class="post-metadata">

**Author:** ![Allan\_Zimmermann](https://discourse.openiap.io/user_avatar/discourse.openiap.io/allan_zimmermann/32/12_2.png) [@Allan\_Zimmermann](https://discourse.openiap.io/u/Allan_Zimmermann)\
**Post date:** [November 22, 2024, 10:11am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/14 "2024-11-22T10:11:58Z")

</div>

In OpenFlow, i check if the \_id and filename exists in the array,  
but since grpc changes that to id i’m unsure if that logic works right now, with NEW workitems …

I need a little time to setup a reproduction of this, but then i can make sure that you CAN add an existing uploaded file to a work item, so you don’t get duplicates.

---

<div class="post-metadata">

**Author:** ![Peter\_Madsen](https://discourse.openiap.io/letter_avatar_proxy/v4/letter/p/bbce88/32.png) [@Peter\_Madsen](https://discourse.openiap.io/u/Peter_Madsen)\
**Post date:** [November 22, 2024, 10:15am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/15 "2024-11-22T10:15:36Z")

</div>

That would be very convientient and a great feature. But we also need to make sure that the unique identifier is accessible through the compenent _file_’s api in the openflow form

---

<div class="post-metadata">

**Author:** ![Allan\_Zimmermann](https://discourse.openiap.io/user_avatar/discourse.openiap.io/allan_zimmermann/32/12_2.png) [@Allan\_Zimmermann](https://discourse.openiap.io/u/Allan_Zimmermann)\
**Post date:** [November 22, 2024, 10:27am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/16 "2024-11-22T10:27:49Z")

</div>

Ah, yeah, forgot to address that.  
So when you upload a file on a form, it needs to be unique, but since the form does not have full context, AND can be reloaded multiple times, I use [forms.io](http://forms.io)’s id in the database. That is why you need to find the file by searching the file(s) by searching for `uniquename` in the file(s) metadata … field

---

<div class="post-metadata">

**Author:** ![system](https://discourse.openiap.io/uploads/default/original/2X/a/a1ca7b164ac9a826839704d9ac82c7b5e6bcf557.png) [@system](https://discourse.openiap.io/u/system)\
**Post date:** [November 29, 2024, 10:28am UTC](https://discourse.openiap.io/t/add-files-to-workitem-in-nodered-based-on-form/1921/17 "2024-11-29T10:28:40Z")

</div>

Closed on November 29, 2024, 10:28am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
