FileInfo to HttpPostedFileBase in mvc controller?

1.5k views Asked by At

I have a web application that has a functionality that consists in put a list of images in a folder (on the same server that the web application) and then process that images to upload them to the web application format.

For that action i have a list of FileInfo objects and i need to convert those FileInfo objects into HttpPostedFileBase because the Upload method uses this kind of object to work.

What's the best way to make this conversion?

1

There are 1 answers

0
usr On

You need to derive from HttpPostedFileBase and implements its members, mainly the Stream member. Return a FileStream. Make sure to dispose of that stream eventually. Leaking file handles on a web server can be deadly.

I would recommend an entirely different approach instead. Don't pass in an HttpPostedFileBase. Pass in a Stream or maybe a custom class. That, of course, requires you to extract the upload logic into a helper method. Don't call the web method Upload, call the helper. That way you don't need to create fake ASP.NET objects.