Skip to main content
Skip table of contents

Upload file via multipart form-data

This guide illustrates how to use a Node-RED function node to construct a request payload for posting file content with the content type multipart/form-data.

image-20240417-064534.png

example: use multipart form-data to upload file

Steps

  1. Importing the File Module:

    Ensure that the File module is accessible within your qibb environment. If it's not already available, you can import it using the following code snippet:

    CODE
    const File = global.get("File");
  2. Converting the File Content:

    Convert the file content into a Buffer object using the appropriate encoding. For example:

    CODE
    const fileBuffer = Buffer.from(myMp3, 'binary');
  3. Specifying File Details:

    Define the filename and file type for the file being uploaded:

    CODE
    const fileName = "blah.mp3";
    const fileType = "audio/mp3";
  4. Constructing the Payload:

    Create the payload object with the file information using the File constructor:

    CODE
    msg.payload = { 
      "file": new File([fileBuffer], fileName, {type: fileType}) 
    };
  5. Returning the Message:

    Assign the payload to the msg object and return it to proceed with further processing:

    CODE
    return msg;

Example

Consider the following example flow where a function node is used to construct the request payload:

JS
// Import File module
const File = global.get("File");

// Convert file content to Buffer
const fileBuffer = Buffer.from(myMp3, 'binary');

// Specify file details
const fileName = "blah.mp3";
const fileType = "audio/mp3";

// Construct payload
msg.payload = {
    "file": new File([fileBuffer], fileName, {type: fileType})
};

// Return the message
return msg;

This payload can then be used in subsequent nodes to post file content with the specified content type.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.