I was working on my website when i noticed my foreach loop wasn't working to upload a list of files and a for loop did work. I am curious to find out why the for loop works and the foreaches loop aren't.
The error message i got was: Cannot cast system.string to xx.
So these are the loops i have:
HttpFileCollection documentsList = Request.Files;
// Doesn't
foreach (var file in documentsList.Cast<HttpPostedFile>())
{
var test = file;
}
// Doesn't
foreach (var file in documentsList)
{
var test = (HttpPostedFile)file;
}
// Works
for (int i = 0; i < documentsList.Count; i++)
{
var file = (HttpPostedFile) documentsList[i];
}
Thanks in advance!