Uploading files to Node-Red

5,346 views
Skip to first unread message

Ian M

unread,
Dec 9, 2015, 10:21:17 AM12/9/15
to Node-RED
Hi,

Just wondering what the current best practice is for uploading a file (an image) from an HTML form to Node-Red.

What I want to be able to do is have a web page hosted within NR that can upload a file to the NR server at which point NR saves it in a directory.

Does anyone have some suggestions as to how I should approach this, or an example?

Thanks,
Ian

Nathanaël Lécaudé

unread,
Dec 9, 2015, 8:41:43 PM12/9/15
to Node-RED

Ian M

unread,
Dec 10, 2015, 11:57:41 AM12/10/15
to Node-RED
I downloaded and installed this as the first thing I tried, but it seems that it is built to allow NR to upload to another web site, not for NR to be the receiving server...

Nicholas O'Leary

unread,
Dec 10, 2015, 12:01:55 PM12/10/15
to Node-RED Mailing LIst
Just as an fyi, the author of that node has kindly renamed it as node-red-contrib-file-upload as per our naming guidelines - http://flows.nodered.org/node/node-red-contrib-file-upload - so the previous link 404s now.

File Upload to node-red has come up a couple times - there isn't a quick and easy solution right now, but it is possible, have a search of the google group for the previous discussion.

Given it has come up a couple times now, I'll see about adding a File Upload mode to the existing HTTP In node (although it may end-up as a new node in the HTTP set of nodes).

Nick

--
http://nodered.org
---
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.
For more options, visit https://groups.google.com/d/optout.

John O'Connor

unread,
Apr 26, 2016, 2:41:42 PM4/26/16
to Node-RED
I know this is a bit of necromancing, but I had the same need and created a modified http node that allows for multi-part form submissions.


It can handle any multi-part form submission and places the files in "msg.req.files".  It's configured by selecting which fields you want to process as files.  Non-file fields are put in msg.body and can be accessed as well.

It's simple but gets the job done.  I have it uploading files automatically to a publicly-viewable Amazon S3 bucket.

Nathanaël Lécaudé

unread,
Apr 27, 2016, 8:54:42 AM4/27/16
to Node-RED
Very interesting, do you have an example flow to go with it ?

Thanks !

Pete Kavanagh

unread,
Oct 5, 2017, 1:00:04 PM10/5/17
to Node-RED
I'm really struggling to get a file upload working within a dashboard.
The examples where you can have a HTTP listener on say /test/upload and the form then redirects you to /test/uploaded etc. - I can't see how to apply these to a dashboard scenario where I want to use the regular /ui URL and have the file upload form within a dashboard template node.

Thanks.

Nick O'Leary

unread,
Oct 10, 2017, 3:11:30 PM10/10/17
to Node-RED Mailing List
Hi Pete,


Here's a quick example that works for me:

Create a ui_template with the following content:

<form action="/file-upload" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

This is a simple html form set to upload its contents (which includes a file input) to the /file-upload url.

Then, I have an HTTP In node set to accept POSTs on /file-upload - and I have ticked the 'Accept file uploads' option.

When I hit the submit button the form, the HTTP In node is triggered with the uploaded file under msg.req.files[0]

Does that help?


[{"id":"90de0cfe.ca90a","type":"http in","z":"d7390dbd.02ae5","name":"","url":"/file-upload","method":"post","upload":true,"swaggerDoc":"","x":543,"y":368,"wires":[["f44eee6a.fa1a8","7835838e.a259cc"]]},{"id":"f44eee6a.fa1a8","type":"debug","z":"d7390dbd.02ae5","name":"","active":true,"console":"false","complete":"req.files","x":859,"y":376,"wires":[]},{"id":"7835838e.a259cc","type":"function","z":"d7390dbd.02ae5","name":"","func":"msg.payload = \"OK\";\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":420,"wires":[["b2930be5.501e38"]]},{"id":"b2930be5.501e38","type":"http response","z":"d7390dbd.02ae5","name":"","statusCode":"","headers":{},"x":750,"y":460,"wires":[]}]



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.
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/cd08d848-126b-482a-87b0-26e916a16031%40googlegroups.com.

Pete Kavanagh

unread,
Oct 11, 2017, 5:46:34 AM10/11/17
to Node-RED
Hi Nick, thanks for replying.  It does help, but is similar to other examples that I've seen in as much as it navigates the user away from the dashboard (in this case to a blank page, apart from the "OK").
Ideally I'd like to keep the user on the dashboard.  Maybe this behaviour is unavoidable, and I just need to redirect somehow back to the dashboard URL, and then have some code that suppresses the form until the processing of the uploaded file is complete.

Pete.

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Nick O'Leary

unread,
Oct 11, 2017, 5:51:51 AM10/11/17
to Node-RED Mailing List
Hi Pete,

in which case, you can use a bit of javascript on the dashboard page to do the form submission...



<form id="myForm">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<script>
   $('#myForm').submit(function(e) {
       e.preventDefault();
    $.ajax({
        url:'/file-upload',
        type:'post',
        data:$('#myForm').serialize(),
        success:function(){
            alert("worked");
        }
    });
   });

</script>



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.

Nick O'Leary

unread,
Oct 11, 2017, 5:56:42 AM10/11/17
to Node-RED Mailing List
Ooops - that works for basic form data, but if you want to include a File something else is needed. Try this:



<form id="myForm">
<input id="myFile" type="file" name="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<script>
    $('#myForm').submit(function(e) {
        e.preventDefault();
        var fd = new FormData();    
        fd.append( 'file', $('#myFile')[0].files[0] );

$.ajax({
  url: '/file-upload',
  data: fd,
  processData: false,
  contentType: false,
  type: 'POST',
  success: function(data){
    alert(data);
  }
});

   });

</script>


Pete Kavanagh

unread,
Oct 11, 2017, 10:49:15 AM10/11/17
to Node-RED
Working perfectly.  Thanks!

Julian Knight

unread,
Oct 12, 2017, 7:35:10 AM10/12/17
to Node-RED
This comes up from time-to-time so I've added as a suggestion for the cookbook with a reference to this thread.

P DM

unread,
Feb 27, 2018, 6:52:08 AM2/27/18
to Node-RED
Hi guys ... I'm pretty new to node-red, and I'm trying to learn through examples ....
I'm running node-red on a VM (Ubuntu based) and as a first application I'm trying to use dashboard to
1) upload a file from a client-pc to node-red
2) upload that file using scp/sftp to a list of devices.

Taking a step by step approach, I first want to try to upload a file from the HTTP-client (PC) to the node-red flow, and came across this discussion.
I do get the file-selection on the UI, but nothing happens if the upload button is pressed (no debug output either ...), so I'm missing something here ..
Would it be possible to include a working flow example based on Nick's last input ?

Thanks & best regards

Patrick
Reply all
Reply to author
Forward
0 new messages