【问题标题】:Non System.Web substitute for HttpPostedFileBase?HttpPostedFileBase 的非 System.Web 替代品?
【发布时间】: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 功能,因为仅使用了其 FileNameSaveAs() 成员。

是否有一个替代品,我可以在调用者处轻松地将HttpPostedFileBase 转换为它,这样我需要作为参数传递的只是一个非网络文件?

注意:HttpPostedFileBase 直接继承自 System.Object,而不是任何文件类。

【问题讨论】:

  • 你不能通过使用FileName来复制附件而不是使用SaveAs方法吗?
  • @MehrzadChehraz 显然他不能。注意他源代码中的file.SaveAs(fileTarget):他也在传递流。
  • @NotSoSharp 那么他需要定义一个接口。

标签: c# asp.net .net system.web


【解决方案1】:

HttpPostedFileBase 是一个抽象类。所以挑战在于你实际上并没有替换那个类,你正在替换HttpPostedFileWrapper,这是实现。 (这不是类继承自什么,而是从它继承什么。)

HttpPostedFileWrapper 又引用其他 System.Web 类,例如 HttpInputStream 和 'HttpPostedFile`。

所以你不能替换它。也许通过要求您不要引用 System.Web 的意图是您正在移动与 Web 功能不直接相关的遗留代码,例如业务逻辑。如果您不能完全不使用代码,也许您可​​以将其从您正在创建的新程序集中删除,然后使用另一个引用 System.Web 的程序集。如果他们不需要这个特定的功能,他们只引用一个程序集,但如果他们需要这个,他们还可以添加第二个引用 System.Web 的程序集。

【讨论】:

  • 谢谢。是否有接受 HttpPostedFileWrapper 的派生参数作为其参数的 FileStream 构造函数?
  • 没有。您可以在此处查看所有构造函数:msdn.microsoft.com/en-us/library/…。 (我经常使用 MSDN 文档来编写类和方法。)
【解决方案2】:

如果您不想引用System.Web 并且还想使用SaveAs 方法,您可以定义一个接口和一个包装器来创建链接。不过,这不是一个非常简单的方法:

//// Second assembly (Without referencing System.Web):
// An interface to link the assemblies without referencing to System.Web
public interface IAttachmentFile {
    void SaveAs(string FileName);
}
..
..
// Define ChangeAttachment method
public string ChangeAttachment(int userId, IAttachmentFile attachmentFile) { 
   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);
   }
   // This call leads to calling HttpPostedFileBase.SaveAs
   attachmentFile.SaveAs(fileTarget);
   return fileTarget;
}

//// First assembly (Referencing System.Web):
// A wrapper class around HttpPostedFileBase to implement IAttachmentFile   
class AttachmentFile : IAttachmentFile {
    private readonly HttpPostedFileBase httpPostedFile;
    public AttachmentFile(HttpPostedFileBase httpPostedFile) {
        if (httpPostedFile == null) {
            throw new ArgumentNullException("httpPostedFile");
        }
        this.httpPostedFile = httpPostedFile;
    }
    // Implement IAttachmentFile interface
    public SaveAs(string fileName) {
        this.httpPostedFile.SaveAs(fileName);
    }
}
..
..
// Create a wrapper around the HttpPostedFileBase object
var attachmentFile = new AttachmentFile(httpPostedFile);

// Call the ChangeAttachment method
userManagerObject.ChangeAttachment(userId, attachmentFile);

【讨论】:

  • 谢谢。不幸的是,这里的接口对我没有帮助,因为我真正需要的是一个从HttpPostedFileBase/HttpPostedFileWrapper 到二进制序列化器的转换器,类似于in this answerthis article 中描述的那个。
  • @datps 我真的不明白你的问题与二进制序列化程序有什么关系。您可能需要更新它以进行澄清。
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
相关资源
最近更新 更多