1

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?

Phoenix_uy
  • 3,173
  • 9
  • 53
  • 100
  • So you want to call the Upload method directly (maybe from a Unit Test)? – usr Jun 17 '15 at 19:29
  • It is not a unit test but yes – Phoenix_uy Jun 17 '15 at 19:34
  • Just to confirm, you are not calling Upload over HTTP, right? I should have asked that right away. – usr Jun 17 '15 at 19:42
  • Exactly.. from the action controller method i'm calling first a method to get the `FileInfo` list with the images and then on the same action i need to call the method `Upload` – Phoenix_uy Jun 17 '15 at 19:46
  • `HttpPostedFileBase` is used in conjunction with uploading a file on the users computer to the server, yet your referring to files already on the server, so it a hard to understand what your trying to do. –  Jun 18 '15 at 01:05

1 Answers1

0

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.

usr
  • 168,620
  • 35
  • 240
  • 369