Marius Grebliauskas Marius Grebliauskas - 16 days ago 8
C# Question

HttpPostedFileBase convert to HttpPostedFile

Is there possibility to convert

HttpPostedFileBase to HttpPostedFile
, i tried searching SO questions, I could only find just opposite situation
HttpPostedFile
to
HttpPostedFileBase
...

Tried reading:
http://www.prideparrot.com/blog/archive/2012/8/uploading_and_returning_files

He states "In some cases we need to convert
HttpPostedFileBase
to
HttpPostedFile
and we can achieve that using the
HttpPostedFileWrapper
.", so how could I do that?

Please do not tell me to use
HttpPostedFileBase
, because I am using DevExpress framework
UploadedFile
class which only accept
HttpPostedFile
in its constructor.

I tried doing like this, but do not know what else should I write
HttpPostedFile


HttpFileCollectionBase files = Request.Files;

for (int i = 0; i < files.Count; i++)
{
var file = files.Get(i);
}


Thanks for Your time and help in advance.

Answer

The solution of my question looks like this:

        List<UploadedFile> uploadedFiles = new List<UploadedFile>();

        var files = Request.Files;

        for (int i = 0; i < files.Count; i++)
        {
            var file = files.Get(i);
            var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
            var obj = (HttpPostedFile)constructorInfo
                      .Invoke(new object[] { file.FileName, file.ContentType, file.InputStream });


            uploadedFiles.Add(new UploadedFile(obj));
        }
Comments