Node-Red 上传的图像损坏

Node-Red Uploaded image gets corrupted

我有一个 Node-RED 流来上传图像并将其保存到文件中。但是图像在 upload/save 上损坏了。我测试了通过文件资源管理器更改文件,我的服务器完美地显示了我的图像。所以问题出在上传部分,当我下载上传的图片时,我的 windows 照片显示“文件类型可能不受支持”。

我的流量:

[{"id":"b561f39b.7a55c","type":"ui_upload","z":"85326e02.d8e2b","group":"e12d112e.2bd9c","title":"Hallenplan (PNG, 1280x600)","name":"","order":3,"width":0,"height":5,"chunk":"256","transfer":"binary","x":130,"y":380,"wires":[["7f8ff192.87745"]]},{"id":"7f8ff192.87745","type":"file","z":"85326e02.d8e2b","name":"","filename":"/tmp/hallenplan.png","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"binary","x":550,"y":380,"wires":[[]]},{"id":"8f232064.9538","type":"http in","z":"85326e02.d8e2b","name":"","url":"/img/hallenplan.png","method":"get","upload":false,"swaggerDoc":"","x":170,"y":440,"wires":[["96426e36.186fb"]]},{"id":"96426e36.186fb","type":"file in","z":"85326e02.d8e2b","name":"","filename":"/tmp/hallenplan.png","format":"","x":400,"y":440,"wires":[["a62d44b7.b91498"]]},{"id":"751ec35b.adf85c","type":"http response","z":"85326e02.d8e2b","name":"","statusCode":"","headers":{},"x":770,"y":440,"wires":[]},{"id":"a62d44b7.b91498","type":"change","z":"85326e02.d8e2b","name":"Set Headers","rules":[{"t":"set","p":"headers","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"headers.content-type","pt":"msg","to":"image/png","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":440,"wires":[["751ec35b.adf85c"]]},{"id":"e12d112e.2bd9c","type":"ui_group","name":"Upload","tab":"acda14fc.fcb598","order":2,"disp":true,"width":"12","collapse":false},{"id":"acda14fc.fcb598","type":"ui_tab","name":"Timo_Burkhardt","icon":"dashboard","disabled":false,"hidden":false}]

问题是 ui-upload 节点将上传的文件分成 256kb 的块。

然后您将文件输出节点设置为在每个传入消息上覆盖文件,这意味着写入磁盘的文件仅包含您上传的图像的最后一块(最大 256kb)。

quick 可能的解决方案可能是在 ui-上传和文件节点之间插入一个连接节点,以将上传重组为一条消息。但我不确定这是否有效,因为我认为它不会输出缓冲区作为合并的有效负载(因为 ui-upload 节点将类型设置为“字符串”)。

这是将任意大小(适合内存)的 png 图像重复上传到 /tmp/image.png 的完整流程。

工作原理:

  • 获取使用节点上传的图像块ui_upload
  • type 设置为 buffer 因为 ui_node 将默认 type 设置为 string
  • 将块加入内存中的 blob
  • 通过覆盖现有文件将 blob 保存到文件

流码:

[{"id":"87b7f497.057208","type":"file","z":"8130dbcf.fc6768","name":"","filename":"/tmp/image.png","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"setbymsg","x":640,"y":220,"wires":[[]]},{"id":"b4191d57.653ad8","type":"join","z":"8130dbcf.fc6768","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":470,"y":220,"wires":[["87b7f497.057208"]]},{"id":"584ef1f0.f312e8","type":"change","z":"8130dbcf.fc6768","name":"Set type=buffer","rules":[{"t":"set","p":"parts.type","pt":"msg","to":"buffer","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":220,"wires":[["b4191d57.653ad8"]]},{"id":"86a479d3.e5fd6","type":"ui_upload","z":"8130dbcf.fc6768","group":"ca0d541e.2985e","title":"upload2","name":"","order":1,"width":0,"height":5,"chunk":256,"transfer":"binary","x":130,"y":220,"wires":[["584ef1f0.f312e8"]]},{"id":"ca0d541e.2985e","type":"ui_group","name":"Konfiguration","tab":"b75d261a.ea0aa8","order":1,"disp":true,"width":"6","collapse":false},{"id":"b75d261a.ea0aa8","type":"ui_tab","name":"Floorplan","icon":"dashboard","disabled":false,"hidden":false}]