Reputation: 1054
I'm using the node module Request
I need to attached several files to the form post. I have the filenames that I need to attach in the following format:
var images = "images1.jpg,image2.jpg,image3.jpg"
I'm not sure how to include them into the form post. According to the docs, you declare the form data like:
var formData = {
attachments: [
fs.createReadStream(__dirname + '/image1.jpg'),
fs.createReadStream(__dirname + '/image2.jpg'),
fs.createReadStream(__dirname + '/image3.jpg'),
],
};
But how do I loop through the content of "images" and use "fs.createReadStream" to dynamically add the images to the form data?
Upvotes: 0
Views: 78
Reputation: 5704
Create an array from your images
images = images.split(',');
Then attach each image
var formData = { attachments: []}
for(var i = 0; i < images.length; i++){
formData.attachments.push(fs.createReadStream(__dirname + '/' + images[i]));
}
Upvotes: 1