【问题标题】:How to instantiate a HttpPostedFile如何实例化 HttpPostedFile
【发布时间】:2011-04-01 14:23:47
【问题描述】:

我正在尝试与我无法控制的系统进行通信,但是它的一个方法采用 HttpPostedFile 在我的代码中我有一个字节数组。有没有人有实例化 HttpPostedFile 的示例,因为我知道它的构造函数是内部的?

我发现的最好的是Creating an instance of HttpPostedFile with Reflection,它使用反射,但是它们被引导到另一个我无法接受的方向,因为我无法修改第三方系统方法签名。

【问题讨论】:

    标签: c# reflection file-upload


    【解决方案1】:

    这真的是很老套的代码,但下面的代码似乎对我有用:

    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;
    }
    

    【讨论】:

    • @paracycle - 在返回的 HttpPostedFile 上调用 .Save(filePath) 时 - 我得到一个空图像。知道为什么吗?
    • @wilsjd:我上次研究这个已经太久了。从那时起,.NET 平台代码可能发生了很大变化。我建议您查看新代码(使用 Reflector 或类似代码)并进行适当的更改。
    • 如果需要保存 HttpPostedFile 还需要调用 DoneAddingBytes 方法:typeHttpRawUploadedContent.GetMethod("DoneAddingBytes", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(uploadedContent, null);
    • @sundog 谢谢,用您的代码更新了示例。
    • 这对单元测试非常有用,为我节省了很多时间!
    【解决方案2】:

    你可以试试

     var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
     var obj = (HttpPostedFile)constructorInfo
               .Invoke(new object[] { "filename", "image/jpeg", null });
    

    obj 的类型为 HttpPostedFile。我将最后一个参数设置为 null,但它必须是 HttpInputStream。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-07
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多