Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor on type 'System.Web.HttpPostedFile' not found

I'm trying to create an instance of HttpPostedFile with

var obj2 = Activator.CreateInstance(
    typeof(HttpPostedFile), 
    BindingFlags.NonPublic | BindingFlags.Instance,
    null,
    new object[] { },
    System.Globalization.CultureInfo.CurrentCulture
);

but I'm getting the error 'Constructor on type 'System.Web.HttpPostedFile' not found.'.

Is there another way to create an instance of HttpPostedFile or am I doing something wrong?

like image 884
Lieven Cardoen Avatar asked May 16 '26 13:05

Lieven Cardoen


2 Answers

I've just had the same problem and solved it using Moq as follows

var postedfile = new Mock<HttpPostedFileBase>();
postedfile.Setup(f => f.ContentLength).Returns(8192);
postedfile.Setup(f => f.FileName).Returns("myfile.txt");
postedfile.Setup(f => f.InputStream).Returns(new MemoryStream(8192));
myObject.HttpPostedFile = postedfile.Object;
like image 143
stevie_c Avatar answered May 18 '26 02:05

stevie_c


You might be able to use System.Web.HttpPostedFileBase instead. This has the same methods as HttpPostedFile but is designed for sub-classing. You can then use HttpPostedFileWrapperto pass in a real HttpPostedFile to a method expecting an HttpPostedFileBase.

These extra classes are in the System.Web.Abstractions assembly. ASP.NET MVC makes use of this (and other abstractions in the same assembly) to make unit testing easier. See the documentation on MSDN for more information.

like image 25
Ben Lings Avatar answered May 18 '26 02:05

Ben Lings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!