skip to Main Content

This is my function in my node JS script:

const uploadFile = async () => {
  const buffer = fs.readFileSync('scripts/pose.png')
  const formData = new FormData()

  formData.append('file', buffer, 'dummyName.png')

  const headers = {
    Authorization: `Bearer ${jwt}`,
    maxBodyLength: Infinity,
    maxContentLength: Infinity,
  }

  const response = await axios.post(
    `${process.env.URL}/v1/files`,
    formData,
    {
      headers
    },
  )

  return response
}

no matter what I’m doing it’s not working… with this code I’m getting an error:
"source.on is not a function"

Can someone help me, I tried literally everything!

2

Answers


  1. I want to be sure i understand your question. but if what you are trying to do is upload an image from a form, you can use multer for it. it’s quite easy to use and understand.

    check them on npmjs here: https://www.npmjs.com/package/multer

    Login or Signup to reply.
  2. 
    const uploadFile = async () => {
      const buffer = fs.readFileSync(
        'scripts/pose.png',
      );
      const formData = new FormData();
    
      formData.append('file', buffer, {
        filename: 'dummyName.png',
        contentType: 'image/jpeg', // Adjust the content type accordingly
      });
    
      const headers = {
        Authorization: `Bearer ${jwt}`,
        'Content-Type': 'multipart/form-data',
        ...formData.getHeaders(),
      };
    
      const response = await axios.post(`${process.env.URL}/v1/files`, formData, {
        headers,
      });
    
      return response;
    };
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search