0

I am trying to use the Bootstrap FileInput with a C# Controller. No matter what I do, I can only seem to get the controller to accept 1 file (even when multiple files are selected)

my HTML:

<form enctype="multipart/form-data" method="post">
<label>Upload Photo </label>
<input id="FilesInput" name="FilesInput" type="file" multiple>
</form>

my Javascript:

  $('#FilesInput').fileinput({
    language: 'eN',
    uploadUrl: '/members/UploadSerivcePhoto',
    allowedFileExtensions: ['jpg', 'png', 'gif'],
});

and my MVC C# Controller

        [HttpPost]
    public void UploadSerivcePhoto(IEnumerable<HttpPostedFileBase> FilesInput)
    {
    //  only 1 file ever gets put in FilesInput. 
    }
3
  • Could you check in UploadSerivcePhoto if Request.Files also contains only 1 file? And if it does, use ur browser's network dev tools, or use Fiddler to check what's the request (POST) that is being sent to your controller. Commented Sep 25, 2015 at 11:46
  • Wouldn't the FileInput transfer files one by one, effectively calling your controller action several times, once for each file? Commented Sep 25, 2015 at 11:49
  • 1
    Possible duplicated, please check this answer :) Commented Sep 25, 2015 at 11:55

2 Answers 2

2

I think that it has something to do with asynchronous uploads, the plugin seems to have a default options to post files asynchronous in parallel so you will receive a single file per post.

So try to set the uploadAsync property to false.

$('#FilesInput').fileinput({
language: 'eN',
uploadAsync: 'false', 
uploadUrl: '/members/UploadSerivcePhoto',
allowedFileExtensions: ['jpg', 'png', 'gif'],

});

Here is some official documentation for Bootstrap Fileinput. Asynchronous Upload

Sign up to request clarification or add additional context in comments.

Comments

1

I had the same issue. Check the Request.Files property as @Michael mentioned, it should contain several files. When you check it, you should also filter out the ones with empty name:

List<HttpPostedFileBase> allFiles = Enumerable.Range(0, Request.Files.Count)
    .Select(x => Request.Files[x])
    .Where(x => !string.IsNullOrEmpty(x.FileName))
    .ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.