1

I am trying to upload multiple files on my web application. Hence I am using IEnumerable<HttpPostedFileBase> class to loop inside each file. But I am getting an error stating -

Error System.Collections.Generic.IEnumerable<System.Web.HttpPostedFileBase> does not contain a definition for 'ContentLength' and no extension method 'ContentLength' accepting a first argument of type System.Collections.Generic.IEnumerable<System.Web.HttpPostedFileBase' could be found (are you missing a using directive or an assembly reference?)

This error is for all the properties present in the HttpPostedFileBase class. I am trying to edit that class but it doesnt allow. I tried creating a IEnumerable of HttpPostedFileBase class in my ViewModel, but it fails again. What am I missing here?

Update - Code: View

<div class="col-sm-8">                                  
 <input type="file" name="Files" id="file1" class="form-control" />
  <input type="file" name="Files" id="file2" class="form-control" />
  <input type="submit" value="Save" class="btn btn-default" name="Command"/>   
</div>

Controller

public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> Files)
{
     foreach (var item in Files)
     { 
           if (Files != null && Files.ContentLength > 0)
           {
                FileUpload up = new FileUpload();
                up.PersonId = model.PersonId;
                up.FileName = System.IO.Path.GetFileName(Files.FileName);
                up.MimeType = Files.ContentType;
                up.FileContent = Files.Content;
                bll.AddFileUpload(up);
            }
     }
     return View();
}
3
  • please share some code that you're using for processing HttpPostedFileBase items because it seems that you are trying to access to collection in a wrong way Commented Sep 19, 2015 at 21:06
  • please see my edited question, i have included the code. thanks Commented Sep 19, 2015 at 21:20
  • msdn.microsoft.com/en-us/library/… Commented Sep 19, 2015 at 21:22

3 Answers 3

2

The problem is here:

foreach (var item in Files)
    if (Files != null && Files.ContentLength > 0)

You're using foreach to iterate the collection, but you still check the IEnumerable called File instead of each item. What you want is:

foreach (var item in Files)
    if (item != null && item.ContentLength > 0)

As a side note, you can filter out items using Enumerable.Where:

foreach (var item in Files.Where(file => file != null && file.ContentLength > 0))
{
    FileUpload up = new FileUpload
    {
         PersonId = model.PersonId,
         FileName = System.IO.Path.GetFileName(item.FileName),
         MimeType = item.ContentType,
         FileContent = item.Content,
     };
     bll.AddFileUpload(up);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You're trying to call .ContentLength on the collection, instead of on a file in that collection. Instead, try something like:

// given postedFiles implements IEnumerable<System.Web.HttpPostedFileBase
foreach(var postedFile in postedFiles) {
 var len = postedFile.ContentLength
}

Comments

1

your code trying to access collection itself, but you need to access to collection item like this:

foreach (var item in Files)
{

FileUpload up = new FileUpload();
up.PersonId = model.PersonId;
up.FileName = System.IO.Path.GetFileName(item.FileName);
up.MimeType = Files.ContentType;
up.FileContent = item.Content;
bll.AddFileUpload(up);
}

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.