您希望创建一个 HTTP 端点,该端点响应 GET 请求,并提供来自本地文件的内容,例如 PNG 图像。
使用 File In
节点加载所需内容,并将 Content-Type
设置为与返回文件类型相应的适当值。
[{"id":"c7e341a0.381cc","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-file","method":"get","swaggerDoc":"","x":110,"y":720,"wires":[["2fb1c354.d04e3c"]]},{"id":"2fb1c354.d04e3c","type":"file in","z":"3045204d.cfbae","name":"","filename":"/tmp/node-red.png","format":"","x":290,"y":720,"wires":[["c9e28681.361d78"]]},{"id":"c9e28681.361d78","type":"change","z":"3045204d.cfbae","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":470,"y":720,"wires":[["88974243.7768c"]]},{"id":"88974243.7768c","type":"http response","z":"3045204d.cfbae","name":"","x":610,"y":720,"wires":[]}]
[~]$ curl https://:1880/hello-file > file.png
加载非文本文件(如图像)时,必须将 File In
节点配置为返回 Buffer
对象。
为了让接收方知道如何处理文件,Content-Type
标头必须设置为相应的 MIME 类型。上面的示例返回一个 .png
文件,它将 Content-Type
标头设置为 image/png
。
版权所有 OpenJS Foundation 和 Node-RED 贡献者。保留所有权利。OpenJS Foundation 已注册商标并使用商标。有关 OpenJS Foundation 商标列表,请参阅我们的商标政策和商标列表。未在OpenJS Foundation 商标列表中指明的商标和徽标是其各自持有者的商标™ 或注册® 商标。使用它们不暗示与其有任何从属关系或获得其认可。
OpenJS Foundation | 使用条款 | 隐私政策 | OpenJS Foundation 章程 | 商标政策 | 商标列表 | Cookie 政策