- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试与我无法控制的系统进行通信,但是它的一种方法采用 HttpPostedFile 在我的代码中我有一个字节数组。有没有人有实例化 HttpPostedFile 的例子,因为我知道它的构造函数是内部的?
我找到的最好的是 Creating an instance of HttpPostedFile with Reflection它使用反射,但是它们被引导到另一个我不能接受的方向,因为我无法修改第三方系统方法签名。
最佳答案
这真的是非常 hacky 代码,但以下似乎对我有用:
public HttpPostedFile ConstructHttpPostedFile(byte[] data, string filename, string contentType) {
// Get the System.Web assembly reference
Assembly systemWebAssembly = typeof (HttpPostedFileBase).Assembly;
// Get the types of the two internal types we need
Type typeHttpRawUploadedContent = systemWebAssembly.GetType("System.Web.HttpRawUploadedContent");
Type typeHttpInputStream = systemWebAssembly.GetType("System.Web.HttpInputStream");
// Prepare the signatures of the constructors we want.
Type[] uploadedParams = { typeof(int), typeof(int) };
Type[] streamParams = {typeHttpRawUploadedContent, typeof (int), typeof (int)};
Type[] parameters = { typeof(string), typeof(string), typeHttpInputStream };
// Create an HttpRawUploadedContent instance
object uploadedContent = typeHttpRawUploadedContent
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, uploadedParams, null)
.Invoke(new object[]{data.Length, data.Length});
// Call the AddBytes method
typeHttpRawUploadedContent
.GetMethod("AddBytes", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(uploadedContent, new object[] {data, 0, data.Length});
// This is necessary if you will be using the returned content (ie to Save)
typeHttpRawUploadedContent
.GetMethod("DoneAddingBytes", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(uploadedContent, null);
// Create an HttpInputStream instance
object stream = (Stream)typeHttpInputStream
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, streamParams, null)
.Invoke(new object[] {uploadedContent, 0, data.Length});
// Create an HttpPostedFile instance
HttpPostedFile postedFile = (HttpPostedFile)typeof(HttpPostedFile)
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null)
.Invoke(new object[] {filename, contentType, stream});
return postedFile;
}
关于c# - 如何实例化 HttpPostedFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5514715/
public async Task UpdateUserProfile(HttpPostedFile postedFile) { //update operations } 我有一个 Update
我有一个表单将 POST 数据发送到 Action,并且一个模型有 HttpPostedFile 字段,但是当我将表单数据发送到 Action 时,我只得到空值 HttpPostedFile 字段 .
我有一个应用程序,允许用户上传文件,该文件在保存之前会使用赛门铁克防护引擎进行扫描。我遇到的问题是,使用保护引擎扫描文件后,它们的字节数为 0。我正在尝试找到解决这个问题的方法。 我已经尝试过这里提到
在这种情况下,用户可能会上传文件并可能会触发垃圾邮件过滤器。如果它确实触发了垃圾邮件过滤器,我会保存所有帖子数据并显示验证码。如果“bot”在 session 超时时没有成功,则 HttpPosted
我已经创建了一个用于多张图片上传的简单机制。 文件正在正确上传到服务器,但是当我在服务器端遇到一些错误时,我执行 ModelState.AddModelError("PostedPhotos", "E
我正在尝试与我无法控制的系统进行通信,但是它的一种方法采用 HttpPostedFile 在我的代码中我有一个字节数组。有没有人有实例化 HttpPostedFile 的例子,因为我知道它的构造函数是
我想使用 HttpPostedFile用于将一个或多个大文件从网页上传到 ASP.NET MVC Controller 的类。使用此类,上传的大于 256 KB 的文件将缓冲到磁盘,而不是保存在服务器
我正在尝试获取上传文件的文件路径,并且我正在使用 HttpPostedFile,但它似乎不是获取路径名的选项。真的吗?我该怎么办? foreach (string s in Request.Files
我关注了这个tutorial在 HttpPostedFileBase 上创建验证,如果我使用 HttpPostedFileBase 就可以了, 但如果我改为 IEnumerable上传多个文件,并提交
是否有可能将 HttpPostedFileBase 转换为 HttpPostedFile,我尝试搜索 SO 问题,我只能找到正好相反的情况 HttpPostedFile 到 HttpPostedFil
我想验证文件类型以确保用户正在上传 JPEG、GIF 或 PNG 类型的图像。我认为使用 HttpPostedFile.ContentType 会更安全,因为它会检查 MIME 内容类型,而不是检查文
在 View 上: 在Controller中,有这样的东西: [AcceptVerbs(HttpVerbs.Post)] public ActionResult About(HttpP
我正在使用具有 FromBinary 方法的图像组件。想知道如何将输入流转换为字节数组 HttpPostedFile file = context.Request.Files[0]; byte[] b
您可以将模型的属性定义为 HttpPostedFile?我正在尝试并且总是作为空值出现。 这是代码 public class New { public string Title { get;
我有一个 aspx 网页用作处理程序来通过 swfupload 保存上传的文件我将文件保存在服务器上,如下所示: HttpPostedFile uploadedFile = Request.Files
我有一个 C#.net web 应用程序,可以将文件(通过 POST 方法)发送到另一个应用程序。在第二个应用程序中,我有以下代码来检索发布的文件。 HttpPostedFile hpf = Requ
是否可以将 byte[] 转换为 HttpPostedFile? System.Web.HttpPostedFile objFile = fileData; 其中fileData是一个图片的字节数组,
如何将 httppostedfile 发布到 webapi?基本上我希望用户选择一个 excel 文件并将其发布到我的 webapi。 gui 是用经典的 asp.net 制作的,webapi 是用新
我正在上传一个文件并直接将 Inputstream 提供给我的一个对象。我的问题是,我们是否应该在完成处理后关闭并刷新它? 最佳答案 您不需要显式关闭它,在请求结束时,您的服务器上分配的资源将被释放。
有人可以提供代码以将上传的文件以二进制形式保存到 azure blob 吗?我目前使用文本保存,对于大文件来说,逐行读取/保存到 blob 的速度非常慢。 Private Function ReadF
我是一名优秀的程序员,十分优秀!