APIs & Integrations

AngelKanna
Participant

Need to upload multiple pdf files using File API- Node

SOLVE

The below code works for a single file upload, but if I have an array of file paths 

filePaths = ['abc.pdf''sample.pdf','xyz.pdf'] all files are in current directory. When I loop over the array, the code just uploads the last file 3 times to hubspot folder. I think fs.createReadStream() should be async. please advise
AngelKanna_0-1739300992719.png

 

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
let data = new FormData();
const filePaths = ['abc.pdf''sample.pdf','xyz.pdf'];

async function toCall(){
for(var i=0i< filePaths.lengthi++){
   await getFormData(filePaths[i]);
let config = {
  method: 'post',
  maxBodyLength: Infinity,
  headers: {
    'Authorization': 'Bearer ***********************************',    
    ...data.getHeaders()
  },
  data : data
};

axios.request(config)
.then((response=> {
  console.log(JSON.stringify(response.data));
})
.catch((error=> {
  console.log(error);
});
}
}

async function getFormData(path)
{
    data.append('file'fs.createReadStream(path));    
    data.append('folderId''**********');
    data.append('options''{"access":"PRIVATE"}');

}

toCall();
 
 
 
 
 
 
0 Upvotes
1 Accepted solution
Jaycee_Lewis
Solution
Community Manager
Community Manager

Need to upload multiple pdf files using File API- Node

SOLVE

Hey, @AngelKanna 👋

Here's what I'd look at:

  • Creating new FormData for each file vs. reusing for the same instance
  • Use proper async/await handling versus mixing with .then()
  • Look into using Promise.all for doing concurrent uploads
  • Add error handling to both the individual files and the batch process

Best,

Jaycee


Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success.
Don't miss this opportunity to connect and grow—reserve your spot today!


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !

View solution in original post

0 Upvotes
2 Replies 2
Jaycee_Lewis
Solution
Community Manager
Community Manager

Need to upload multiple pdf files using File API- Node

SOLVE

Hey, @AngelKanna 👋

Here's what I'd look at:

  • Creating new FormData for each file vs. reusing for the same instance
  • Use proper async/await handling versus mixing with .then()
  • Look into using Promise.all for doing concurrent uploads
  • Add error handling to both the individual files and the batch process

Best,

Jaycee


Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success.
Don't miss this opportunity to connect and grow—reserve your spot today!


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes
AngelKanna
Participant

Need to upload multiple pdf files using File API- Node

SOLVE

Yes, I used async/await and error handling, so I can upload each file perfectly. Thanks for the suggestions, it helped me to reach a perfect solution

0 Upvotes