While trying to implement an MVC file upload example on Scott Hanselman's blog. I ran into trouble with this example code:
foreach (string file in Request.Files)
{
HttpPostedFile hpf = Request.Files[file] as HttpPostedFile;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
}
I converted it to VB.NET:
For Each file As String In Request.Files
Dim hpf As HttpPostedFile = TryCast(Request.Files(file), HttpPostedFile)
If hpf.ContentLength = 0 Then
Continue For
End If
Dim savedFileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(hpf.FileName))
hpf.SaveAs(savedFileName)
Next
But I am getting an invalid cast exception from the compiler:
Value of type 'System.Web.HttpPostedFileBase' cannot be converted to 'System.Web.HttpPostedFile'.
Hanselman posted his example on 2008-06-27, and I assume it worked at the time. MSDN doesn't have any similar examples, so what gives?