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/pngfor PNGs). For a more robust solution, you could use themimemodule, but this works for starters. - Sync vs Async:
fs.readFileSyncis simple for beginners, but if you're working with large files, you might want to switch tofs.readFilewith 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
- Deploy your flow.
- Drop a test file into your watched folder.
- Use a Debug node connected to the HTTP Request node's output to check the server response.
- If you run into issues:
- Verify the file path in
msg.payloadis 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.
- Verify the file path in
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

