asp.net – 如何从System.Web.HttpPostedFileBase转换为System.W
发布时间:2020-12-15 22:59:43 所属栏目:asp.Net 来源:网络整理
导读:在Scott Hanselman博客上尝试实现一个MVC文件上传 example.我遇到了这个示例代码的麻烦: foreach (string file in Request.Files){ HttpPostedFile hpf = Request.Files[file] as HttpPostedFile; if (hpf.ContentLength == 0) continue; string savedFileN
在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没有任何类似的例子,所以给出了什么? 解决方法
只需使用它作为一个HttpPostedFileBase.该框架使用HttpPostedFileWrapper将HttpPostedFile转换为HttpPostedFileBase的对象. HttpPostedFile是那些难以单元测试的密封类之一.我怀疑在写了例子之后,他们应用了包装代码来提高在MVC框架中测试(使用HttpPostedFileBase)控制器的能力.控制器上的HttpContext,HttpRequest和HttpReponse属性已经完成了类似的操作.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET Web-API不序列化readonly属性
- asp.net-mvc-4 – Visual Studio 2012 Professional中没有M
- asp.net – 如何将@Model传递给Angular ng-init
- 优化 – MVC4捆绑策略
- asp.net – GetHashCode在这里只是货物崇拜吗?
- ASP.Net 2中的上传文件在哪里?
- asp.net-mvc – 没有区域的ASP.NET Web API中的多级路由
- ASP.NET捆绑和分类 – 包括用于生产捆绑包和未压缩文件的已
- asp.net-mvc-3 – 后退按钮或导航到特定视图(页面)-ASP.NET
- asp.net-mvc – MVC HTML.RenderAction – 错误:持续时间必
推荐文章
站长推荐
- 在ASP.NET MVC中为JavaScript生成动作URL
- asp.net – 冻结标题,滚动GridView
- Asp网络核心 – 从模型到javascript
- asp.net-mvc – MVC 4安装失败
- asp.net-mvc – 过滤Kendo UI MVC Grid时的自定义
- asp.net – Azure服务结构:IReliableQueue
- 如何在ASP.NET 5和Visual Studio 2015 CTP中添加
- asp.net – 如何防止asp:Timer在生成响应之前发
- asp.net-mvc-3 – ASP.NET MVC 3将无法识别.csht
- asp.net-mvc – ASP.NET MVC 4 – 用户组的唯一路
热点阅读