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 typeSystem.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();
}
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);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments