- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我了解 HttpPostedFileBase
和 HttpPostedFileWrapper
之间的关系,就两者的需求而言(即在单元测试/模拟中)。但是,为什么当我在 HttpPostedFileBase
的返回值上设置断点时,它是否显示为 HttpPostedFileWrapper
?
此外,HttpPostedFileBase
没有实现 ContentType 属性。那么,当我的代码仅 引用HttpPostedFileBase
而不是HttpPostedFileWrapper
时,为什么它会返回一个值?这是什么诡计?
编辑#1:
感谢@lawliet29 的精彩回复。我已经按照建议写出了结构。
public sealed class MyHttpPostedFile
{
public string ContentType { get { return "123"; } }
}
public abstract class MyHttpPostedFileBase
{
}
public class MyHttpPostedFileWrapper : MyHttpPostedFileBase
{
private MyHttpPostedFile _myHttpPostedFile;
public MyHttpPostedFileWrapper(MyHttpPostedFile myHttpPostedFile) {
_myHttpPostedFile = myHttpPostedFile;
}
public string ContentType { get { return _myHttpPostedFile.ContentType; } }
}
为了让它工作,我需要像这样传递参数:
GetFiles(new MyHttpPostedFileWrapper(new MyHttpPostedFile());
这似乎是我质疑的诡计存在的地方。 .NET 如何知道传递给它的字节是 MyHttpPostedFile
类型的类,并且它应该获取该对象并将其作为参数传递到我的构造函数中?
编辑#2:
我没有意识到 ASP.NET MVC 绑定(bind)器除了通过传递这些更高级别的对象来传递字节之外还能做更多的事情。这就是我想知道的诡计!感谢您的精彩回复。
最佳答案
其实很简单。 HttpPostedFileBase 是一个抽象类,仅用于被派生的目的。它被用来使密封类 HttpPostedFile 中的某些东西可以模拟。
然而,在现实生活中,HttpPostedFile 是处理已发布文件的工具,为了保持一致,创建了 HttpPostedFileWrapper。此类通过包装 HttpPostedFile 为 HttpPostedFileBase 提供实现。
所以HttpPostedFileBase是一个统一的抽象,HttpPostedFile是一个表示posted文件的类,HttpPostedFileWrapper是HttpPostedFileBase的实现,它包装了HttpPostedFile。HttpPostedFileWrapper 的 ContentType 属性的实现从底层 HttpPostedFile 读取内容类型。
编辑:某种解释
ASP.NET MVC 接收到一个文件,并且在其下方的某处创建了一个 HttpPostedFile 实例,因为这是自 .NET Framework 1.0 以来的工作方式。 HttpPostedFile 的定义如下所示:
public sealed class HttpPostedFile
这基本上意味着它不能被继承,也不能为单元测试而模拟。
为解决此问题,ASP.NET MVC 开发人员创建了一个可模拟的抽象 - HttpPostedFileBase,其定义如下:
public abstract class HttpPostedFileBase
现在,您可以定义 MVC 操作,以便它们接受 HttpPostedFileBase 而不是不可模拟的 HttpPostedFile:
[HttpPost]
public ActionResult PostFile(HttpPostedFileBase file)
{
// some logic here...
}
问题是,在下面的某个地方,表示已发布文件的唯一方法是老式的死板 HttpPostedFile。因此,为了支持这种抽象,MVC 开发人员创建了一个名为 HttpPostedFileWrapper 的装饰器,大致如下所示:
public class HttpPostedFileWrapper : HttpPostedFileBase
{
private HttpPostedFile _httpPostedFile;
public HttpPostedFileWrapper(HttpPostedFile httpPostedFile) {
_httpPostedFile = httpPostedFile;
}
public string ContentType { get { return _httpPostedFile.ContentType; } }
// implementation of other HttpPostedFileBase members
}
因此,现在 HttpPostedFileWrapper 是您在使用已发布的文件执行真正的 HTTP POST 请求时实际得到的。由于多态性,您可以将派生类的实例 - HttpPostedFileWrapper - 传递给方法接受基类 - HttpPostedFileBase。
一直以来,您都可以创建自己的模拟实现,比如说,看起来就像正在发布的视频文件。你会这样做
public class MockPostedVideoFile : HttpPostedFileBase
{
public string ContentType { get { return "video/mp4"; } }
// rest of implementation here
}
另一个编辑:HttpPostedFile 的实际实例化全部由 System.Web 为您处理。 ASP.NET MVC 绑定(bind)器对于发布的表单数据非常智能。它会自动检测到某些 post 值实际上是文件的字节,因此为了正确表示它们,它可以使用 System.Web 框架中的旧内容来创建一个实例 HttpPostedFile。
这里的要点是 - 您无需担心。这里的幕后发生了很多事情,我们真的需要感谢 ASP.NET MVC 团队将所有这些低级别的事情抽象化。
您唯一需要担心的地方是单元测试。在您的测试中,您可以使用模拟实现来调用您的操作,如下所示:
myController.PostFile(new MockPostedVideoFile())
关于c# - HttpPostedFileBase 与 HttpPostedFileWrapper 的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50545216/
在我的 MVC 代码中,我得到了一个 httppostedfilebase 类型的图像。我的 SQL 数据库中有相应的图像类型列。 我需要知道如何在我的数据库中将此 httppostedfilebas
在我的 MVC 代码中,我得到了一个 httppostedfilebase 类型的图像。我的 SQL 数据库中有相应的图像类型列。 我需要知道如何在我的数据库中将此 httppostedfilebas
我有一个单元测试,我正在模拟 HttpPostedFileBase 文件并将它们放在列表中。单元测试然后像这样调用邮件服务(不相关的参数,如 to、from、subject 被忽略) [Test] p
我正在尝试使用 MVC 实现上传附件功能。我实际执行上传/保存附件的方法需要 HttpPostedFileBase 类型。 public virtual string Upload(HttpPoste
我有 Kendo ThemeBuilder,在 MVC C# 中创建一个主题,因为我有一个模型,其中包含从 themebuilder 返回的 css body 要将它保存到 css 文件,我可以通过创
我在 HttpPostedFileBase 上遇到错误: The type or namespace name 'HttpPostedFileBase'could not be found(are y
我在带有 CSLA 的分布式环境中使用 MVC .NET,我可以从我的网络层之一(例如 Website.MVC)引用 HttpPostedFileBase,但我不能从单独的层(我们称之为 OtherL
在我的Create View中我有 @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, encty
我正在使用 ASP.NET MVC,并且我有一个上传文件的操作。文件正在正确上传。但我想要图像的宽度和高度。我想我需要先将 HttpPostedFileBase 转换为 Image 然后继续。我该怎么
在我的Create View 我有 @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, encty
我的客户端javascript代码是 function SendMail() { debugger; var decpr = tinyMCE.get('taskdesc
我正在开发一个 ASP.NET MVC 4 应用程序,在许多 View 中用户可以上传我保存在服务器上的文件。除此之外,我还有一个单独的实体,用于保存上传文件的不同数据,例如: string fi
如果我禁用客户端验证 并尝试上传一个大约 11 MB 的文件,将文件设置为 HttpPostedFileBase memeber [ValidateFile] public Htt
我正在使用 C# 开发一个 ASP.NET MVC 项目,试图将文档从表单上传到我的数据库。我目前在我的 Controller 中,我已将文件导入为 HttpPostedFileBase,并且必须将其
有没有一种简单的方法可以从 HttpPostedFileBase 中获取 FileInfo 对象?我意识到我可以保存文件,然后执行类似 DirectoryInfo.GetFiles 的操作,然后循环遍
那么,场景是这样的:用户上传文件,我的代码将此文件转换为字节数组,然后将该数组传递给外部 API。这很好用。 问题是这个文件包含了像æ,ø,å这样的特殊字符,当byte[]再次转换成字符时,这些字符被
我无法弄清楚我做错了什么,但由于某种原因 HttpPostedFileBase 总是返回 null。当我尝试在服务器端上传文件时,我继续使用 HttpPosterFileBase 获取 null:这是
我正在尝试创建一种方法,我可以在其中传递图像文件并检索该图像文件的字节,以便稍后我可以将字节存储在数据库中。这是我的方法代码。 private byte[] GetImageBytes(HttpPos
我正在尝试在 ASP.NET MVC 中上传单个 .csv 文件。在我的 .ascx 文件中,我有:  
我必须使用具有如下方法的网络服务: SubmitUser(UserReg user, HttpPostedFileBase image) { // webservice side process
我是一名优秀的程序员,十分优秀!