在Scott Hanselman博客上尝试实现一个MVC文件上传
example.我遇到了这个示例代码的麻烦:
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); }
我把它转换成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
但是我从编译器得到一个无效的转换异常:
Value of type 'System.Web.HttpPostedFileBase' cannot be converted to 'System.Web.HttpPostedFile'.
汉斯曼在2008-06-27发布了他的例子,我认为它在当时工作. MSDN没有任何类似的例子,所以给出了什么?