-1

I am working on ASP.NET MVC web app.I want to upload multiple files on single input control. In my view:

<input type="file" name="files" multiple="multiple">

My model:

public HttpPostedFileBase[] files { get; set; }

Now, in my controller, I am trying to access each file as:

if (files[0] == null)
{
}
if (files[1] == null)
{
}

But if object is not present at that index, it's giving exception:

"Index was outside the bounds of the array."

So how to check if object is present at that index? I cannot use foreach because I want to treat each file separately. So is there any other option than foreach to do this?

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Jass
  • 341
  • 3
  • 12
  • If `files` doesn't contain any elements, calling either `files[0]` or `files[1]` will throw an exception as well as if `files` is null. You forgot to mention the exception you are throwing as well. Essentially what you're doing now with `files[0] == null` you are checking if the object at that index is null, not if an object exist at that index. – Trevor May 12 '21 at 18:38
  • @Jass: What do you mean "cannot use `foreach` because I want to treat each file separately"? Do you want to treat each file depend on index? If YES than use `for(int =0; i < files.Lenght; i++) { if (file[i] != null) switch(i): /* etc */ }` – Jackdaw May 12 '21 at 18:42
  • @zaggler, files is not null. It has 3 files. Exception is "Index was outside the bounds of the array." – Jass May 12 '21 at 18:43
  • @Jackdaw, yes, kind of. I want to check if each file is present in DB. – Jass May 12 '21 at 18:44
  • @zaggler, Yes I want to check if object at index is null – Jass May 12 '21 at 18:45
  • How to check if it exists – Jass May 12 '21 at 18:45
  • @Jass please update your post to include the error you are getting. – Trevor May 12 '21 at 18:47
  • `Index was outside the bounds of the array`, please [see](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) that post. – Trevor May 12 '21 at 18:48
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Trevor May 12 '21 at 18:49
  • No @zaggler. I have updated question – Jass May 12 '21 at 18:50
  • How does that post (in link [above](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f)) *not answer your issue*? Please explain in details. – Trevor May 12 '21 at 18:52
  • Where is answer to this? "How to check if object is present at that index" – Jass May 12 '21 at 18:55
  • Can't you just check length of array ? if ( files .length >0 ) {...} – rekna May 12 '21 at 18:58
  • I want to take max 7 inputs. I have to check if the value is present at each index upto 6@rekna – Jass May 12 '21 at 19:00
  • Add files.length>0 for the first, files.length>1 for the second.. but also can't see why you couldn't use a for loop for this ... – rekna May 13 '21 at 09:18

1 Answers1

0

If you are going to treat the files differently, then I assume you know how to Identity each of the files separately. If this is so, why not name the files, something like this.

//javascript
if (this) {

//code to identify what file is what

fileData.append("ticket", this.files[0]);//assuming you have identified it as so
fileData.append("passport", this.files[1]);//assuming you have identified it as so

$.ajax({
    url: '/ctrl/Upload',
    type: "POST",
    async: true,
    contentType: false, // Not to set any content header  
    processData: false, // Not to process data  
    data: fileData,

    success: function (result) {
        //success
    },
    error: function (err) {
        console.log(err);
    },
    complete: function () {
        //complete
    }
});
}

Then from your controller you can do this

//c#
public ActionResult Upload()
{
    var files = System.Web.HttpContext.Current.Request.Files;
    var result = UploadFile(currentUser, files);
}
UploadFile(HttpPostedFileBase[] files)
{
    string formKey1 = "ticket";
    string formKey2 = "passport";

    var file1 = System.Web.HttpContext.Current.Request.Files[formKey1];
    var file2 = System.Web.HttpContext.Current.Request.Files[formKey2];

    {
        //treat file 1
    }

    {
        //treat file 2
    }
}