Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
Convert file binary to FormData server-side using NodeJS

I'm building a web application that requires to me to receive file binaries on a server-side endpoint, convert those binaries to FormData, then POST them to another endpoint. The second endpoint only accepts "multipart/form-data".

I'm using axios to create the requests, and NodeJS on the server.

The files are posted from the client as form data, but I'm unable to manipulate them as such once they reach the server.

How do I compel file binaries to FormData in my server-side scripts?

Client:

const handleImageSubmit = async (attachments) => {
    try {
      let image
      if (attachments.length) {
        const formData = new FormData()

        for (let i = 0; i < attachments.length; i += 1) {
          formData.append(
            `files.${attachments[i].name}`,
            attachments[i],
            attachments[i].name
          )
        }

        const config = {
          headers: { 'Content-Type': 'multipart/form-data' },
        }

        image = await axios.post(
          '/api/proxy/upload',
          formData,
          config
        )
      }
      console.log('Files dispatched to server')
    } catch (err) {
      console.error(err)
    }
  }

Server-side:

const upload = async (req, res) => {
  try {
    const data = new FormData()

   // Convert the binaries in req.body into FormData
   const myFiles = ...

    data.append('files',
      myFiles)

    const config = {
      method: 'post',
      url: `${process.env.API_URL}/upload`,
      headers: {
        Authorization: `Bearer ${req.headers['auth-token']}`,
        ...data.getHeaders()
      },
      data: data,
    }

    const signal = await axios(config)
    res.json(signal.data)

  } catch (err) {
    console.error(err)
    res.status(fetchResponse?.status || 500).json(err)
  }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!