在Node-RED中,如何上传到具有给定配置的节点并在以后检索配置? [英] In Node-RED, how do I upload to a node with the given configuration and retrieve the configuration later?

查看:193
本文介绍了在Node-RED中,如何上传到具有给定配置的节点并在以后检索配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Bluemix上使用Node-RED,我想让用户上传文档,这是流的函数/模板中的相关代码片段

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>

当我运行它时,我选择了一个文件并按提交",但随后出现消息 Cannot POST /upload 然后,我去了 http://flows.nodered.org/node/node-red-contrib-http-multipart ,在示例中显示为

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" }]

并使用以下功能在节点的out端口上访问文件

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)如何上传具有配置的节点?


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

2)获得文件名后,如何检索该文件名以发送到下一个服务? -下一个服务是转换"-它接受文件名.

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-经典的http节点,连接到放置表单的html节点:

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-然后,将您的HTTP多部分节点放入Fields:

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;

您将需要在/app/public/
下有一个名为"upload"的文件夹 您还将需要'fs':
在functionGlobalContext的bluemix-settings.js中添加fs:require('fs')
在package.json中,添加"fs":"x.x"
该文件将复制到此处:/app/public/upload/testUpload
然后,我们将能够通过下一个节点中的msg.localFilename访问它,例如在这样的HTML页面中:

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>

这篇关于在Node-RED中,如何上传到具有给定配置的节点并在以后检索配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆