You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node-RED监听文件夹并上传文件至服务器的实现求助

Hey there! Great job getting the folder watch set up with node-red-contrib-wfwatch—you're already most of the way there. Let's walk through exactly how to turn that file creation event into a form-data upload via the HTTP Request node:

Step 1: Confirm the wfwatch payload

First, make sure your wfwatch node is outputting the full path of the newly created file in msg.payload. You can add a Debug node right after it to verify—you should see something like /home/user/your-watch-folder/new-document.pdf when a file is added.

Step 2: Add a Function node to prepare the form data

You'll need a Function node between the wfwatch and HTTP Request nodes to format the file into the structure the HTTP Request node expects for multipart form data. Paste this code into the Function node:

// Import Node.js core modules (no extra installs needed!)
const fs = require('fs');
const path = require('path');

// Grab the full file path from the watcher's payload
const filePath = msg.payload;
// Extract just the filename (e.g., "new-document.pdf")
const fileName = path.basename(filePath);

// Read the file content as a Buffer (required for form data)
const fileBuffer = fs.readFileSync(filePath);

// Build the form data array
// Each entry corresponds to a field in your form
msg.payload = [
    {
        name: "uploaded_file", // Match this to the field name your server expects!
        value: fileBuffer,
        options: {
            filename: fileName,
            contentType: "application/octet-stream" // Use specific types like "image/jpeg" if needed
        }
    }
    // Add extra form fields here if your server requires them, e.g.:
    // { name: "uploader", value: "Node-RED" }
];

// Set the content type (the HTTP node will auto-handle boundaries, but this helps)
msg.headers = { "Content-Type": "multipart/form-data" };

return msg;

Quick notes on this code:

  • Field name: Replace "uploaded_file" with the exact field name your server API is configured to accept for file uploads.
  • Content-Type: If you know the file type (images, PDFs, etc.), you can set a more specific type instead of application/octet-stream (e.g., image/png for PNGs). For a more robust solution, you could use the mime module, but this works for starters.
  • Sync vs Async: fs.readFileSync is simple for beginners, but if you're working with large files, you might want to switch to fs.readFile with a callback/promise to avoid blocking the Node-RED flow.

Step 3: Configure the HTTP Request node

Now set up your HTTP Request node with these settings:

  • Method: POST
  • URL: Your server's file upload API endpoint
  • Payload type: Multipart form data (this is critical—don't miss this!)
  • Authentication: Add any auth headers or credentials here if your server requires them (e.g., API keys, basic auth)

Step 4: Test and debug

  1. Deploy your flow.
  2. Drop a test file into your watched folder.
  3. Use a Debug node connected to the HTTP Request node's output to check the server response.
  4. If you run into issues:
    • Verify the file path in msg.payload is correct (check Debug output).
    • Make sure Node-RED has permission to read the file (common on Linux/macOS if the folder is restricted).
    • Double-check that the field name in the Function node matches what your server expects.

That should do it! You'll have files automatically uploaded as form data whenever they're added to your watched folder.

内容的提问来源于stack exchange,提问作者Jaydipsinh Vaghela

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:03:24