In Node-RED, how do I upload to a node with the given configuration and retrieve the configuration later?

旧巷老猫 提交于 2019-12-11 00:44:46

问题


I am using Node-RED on Bluemix, I want to let the user upload a document, here is the relevant code fragment in a function/template of a flow

<form action="/upload" method="POST">
     <h1>Upload PDF</h1>
<input type="file" name="myFile" />
<input type="submit" />
</form>

When I run it, I chose a file and press 'submit', but then comes the message Cannot POST /upload Then I went to http://flows.nodered.org/node/node-red-contrib-http-multipart , in the example there it says


You can upload to a node with the following configuration:

[{ "name": "myFile" }]

and access the files using the following function on the out port of the node

var fields = msg.req.fields;
msg.fields = Object.keys(fields);
var myFile = fields["myFile"][0];
msg.localFilename = myFile.path
...

1) How can I upload a node with the configuration?

2)Once I get the file name, how do I retrieve it to be sent to the next services? -the next service is 'Conversion' - it takes in the file name.


回答1:


To make it work, you need :
1- a classic http node connected to a html node where you put your form :

<form enctype="multipart/form-data" action="/fileUploaded" method="POST">
        <input type="file" name="myFile" />
        <input type="submit" />
</form>

2- Then you put your HTTP multipart node with Fields :

[{ "name": "myFile"}]

You link that node to a function node with the following code :
var fields = msg.req.files;
msg.fields = Object.keys(fields);
var myFile = fields["myFile"][0];
var fs = global.get('fs');
var inStr = fs.createReadStream(myFile.path);
var outStr = fs.createWriteStream('/app/public/upload/testUpload');
inStr.pipe(outStr);
msg.localFilename ='/upload/testUpload'
return msg;

You will need to have a folder named "upload" under /app/public/
You will also need 'fs' :
In bluemix-settings.js in functionGlobalContext add fs:require('fs')
In package.json, add "fs":"x.x"
The file will be copied there : /app/public/upload/testUpload
Then we will be able to access it via msg.localFilename in the next node, for example in a HTML page like this :

<html>
    <body>
        <h1>Job Done</h1>
        <a href=".{{localFilename}}">File uploaded here</a>
    </body>
</html>



回答2:


  1. install multer for node-red and then restart node-red.

npm install node-red-contrib-http-multipart

  1. After you are done with the installation correctly, you will see httpInMultipart Node.
  2. basic html file upload code:

    <form action="/upload" enctype="multipart/form-data" method="POST">
        <input type="file" name="myFile" />
        <input type="submit" />
    </form>
  1. httpInMultipart->Function->Http Response Node

Configure the httpInMultipart Node: method: POST url: /upload Fields: [ { "name": "myFile"} ]

In function node Write the following code:

var file = msg.req.files;
var filesize
msg.test=file;
var localFilenamePath = file.myFile[0].path;
var fileSize = file.myFile[0].size;
var response={};
if(msg.test)
    {
        response.statusCode=0;
        response.localFilenamePath=localFilenamePath;
        response.fileSize = fileSize;
        response.sendMessage="Successful";
    }
else
    {
        response.statusCode=1;
        response.sendMessage="Failed";
    }
msg.payload=response;
return msg;

And Voila! We are done!



来源:https://stackoverflow.com/questions/40390822/in-node-red-how-do-i-upload-to-a-node-with-the-given-configuration-and-retrieve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!