question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot upload multiple files without "[]"

See original GitHub issue

Summary

I tried to upload multiple files using requestjs but only the last file in the array was uploaded unless I added “[]” after the key.

Simplest Example to Reproduce

var formData = {
  attachments: [
    fs.createReadStream(__dirname + '/attachment1.jpg'),
    fs.createReadStream(__dirname + '/attachment2.jpg')
  ]
};
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
  if (err) {
    return console.error('upload failed:', err);
  }
  console.log('Upload successful!  Server responded with:', body);
});

Expected Behavior

Server should receive two files.

Current Behavior

Only the last file in the array was uploaded.

Possible Solution

Now I could only change the key from “attachments” to “attachments[]” to avoid this problem. But I think this is a bug because the doc says it is ok to use “attachment” as the key to upload multiple files.

software version
request 2.81.0
node 6.11.2
npm 3.10.10
Operating System macOS

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
jeremy-j-acksocommented, Aug 17, 2018

I ended up having to use the “advanced method” in https://github.com/request/request#forms.

var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
var form = r.form();
form.append('file[]', fs.createReadStream(__dirname + '/file1.jpg'));
form.append('file[]', fs.createReadStream(__dirname + '/file2.jpg'));
r();

Would be nice to just be able to pass an Array or something.

0reactions
stale[bot]commented, Aug 17, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Upload Multiple Documents" is disabled in Document Library
Workaround. In the Document Library, under Library Tools click on Datasheet View, then click on Standard View. The Upload Multiple Documents command should...
Read more >
Cannot upload multiple files - javascript - Stack Overflow
I have a small part of my app that allow users to upload 5 different images and as soon as an image has...
Read more >
i cannot upload multiple files in web - Issues - GitLab
Hi all. i just started to try to use GITLAB and i just discovered you cannot upload multiple files in web i been...
Read more >
Cannot upload multiple files to my website any more. Used to ...
Cannot upload multiple files to my website any more. Used to be able to, now it's one at a time. WHat happened? IE...
Read more >
File Upload related list in console cannot upload multiple files ...
Currently it is not supported from the console view. Please log an idea for this. Thanks. Suman. Expand Post.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found