I am trying to move some C#
MVC legacy code into a shared DLL. All went well so far, but I was asked that that shared DLL need not reference System.Web
in any way.
The only type used in that DLL from System.Web is HttpPostedFileBase
:
public string ChangeAttachment(int userId, HttpPostedFileBase file)
{
string attachmentsFolderPath = ConfigurationManager.AppSettings["AttachmentsDirectory"];
if (!Directory.Exists(attachmentsFolderPath))
{
Directory.CreateDirectory(attachmentsFolderPath);
}
string fileTarget = Path.Combine(attachmentsFolderPath, userId.ToString() + Path.GetExtension(file.FileName));
if (File.Exists(fileTarget))
{
File.Delete(fileTarget);
}
file.SaveAs(fileTarget);
return fileTarget;
}
As you can see, no HTTP or Web functionality is needed here, as only its FileName
and SaveAs()
members are used.
Is there a substitute that I can easily convert HttpPostedFileBase
to it at the caller, so that all I need to pass as a parameter is a non-web file?
Note: HttpPostedFileBase
inherits directly from System.Object
, not from any file class.