【发布时间】:2016-03-27 15:35:07
【问题描述】:
我正在尝试将一些 C# MVC 遗留代码移动到共享 DLL 中。到目前为止一切顺利,但有人要求我共享的 DLL 不需要以任何方式引用 System.Web。
System.Web 的 DLL 中使用的唯一类型是 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;
}
如您所见,此处不需要 HTTP 或 Web 功能,因为仅使用了其 FileName 和 SaveAs() 成员。
是否有一个替代品,我可以在调用者处轻松地将HttpPostedFileBase 转换为它,这样我需要作为参数传递的只是一个非网络文件?
注意:HttpPostedFileBase 直接继承自 System.Object,而不是任何文件类。
【问题讨论】:
-
你不能通过使用FileName来复制附件而不是使用SaveAs方法吗?
-
@MehrzadChehraz 显然他不能。注意他源代码中的
file.SaveAs(fileTarget):他也在传递流。 -
@NotSoSharp 那么他需要定义一个接口。
标签: c# asp.net .net system.web