Melanie
Melanie

Reputation: 3111

Get HttpInputStream in C#

I'm working with a C# class for sending emails that expects a List of HttpPostedFiles as one of its method's parameters (for file attachment to an email). I have a file (on the file system) that I want to attach to an email in my program and I'm using this class to do it. My problem (I think) is that in the below code:

var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];    
HttpPostedFile postedFile = (HttpPostedFile)constructorInfo.Invoke(new object[] { pathString + "Policy_Assoc_home_computer_V8.pdf", "application/pdf", httpStream });

httpStream is supposed to be of type HttpInputStream. I don't know how to instantiate such a thing. I've tried the following:

string pathString = Server.MapPath("~/Policy/");
                FileStream stream = new FileStream(pathString + "Policy_Assoc_home_computer_V8.pdf", FileMode.Open);
                Assembly systemWebAssembly = typeof(HttpPostedFile).Assembly;
                Type typeHttpRawUploadedContent = systemWebAssembly.GetType("System.Web.HttpRawUploadedContent");
                Type typeHttpInputStream = systemWebAssembly.GetType("System.Web.HttpInputStream");
                Type[] uploadedParams = { typeof(int), typeof(int) };
                Type[] streamParams = { typeHttpRawUploadedContent, typeof(int), typeof(int) };
                object uploadedContent = typeHttpRawUploadedContent
                    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, uploadedParams, null)
                    .Invoke(new object[] { stream.Length, stream.Length });
                object httpStream = (Stream)typeHttpInputStream
                    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, streamParams, null)
                    .Invoke(new object[] { uploadedContent, 0, stream.Length });

but it throws an error on the line that instantiates uploadedContent:

object of type System.Int64 cannot be converted to System.Int32

How do I do this?

Upvotes: 0

Views: 286

Answers (0)

Related Questions