c# - 实现文件上传的 MVP 模式

标签 c# asp.net file-upload webforms mvp

我是 MVP 模式的新手。我不太清楚在哪里编写将文件上传到服务器的逻辑,然后在上传后执行一些验证步骤和数据库操作。请指教。

public interface IPresenter
{
    void UploadFile(string fileName); //For simplicity, file name is passed;
                                      //it can be declared as a property  
}    

public interface IDefaultView
{
     void Upload();
}

public class DefaultPresenter : IPresenter
{
     protected DefaultPresenter() {}

     public DefaultPresenter(IDefaultView defaultview)
     {
        this.defaultView = defaultView;
     }

     public void UploadFile(string fileName)
     {
         //Do I need to write saving functionality here or call
         //defaultView.Upload(fileName)?
         //
         //From online articles, I have seen all business functionality written here.
         //But I think I will not be able to use System.Web in this layer.
     }
}

public partial class UploadExtract: IDefaultView    
{
    public void Upload(fileName)
    {                
    }
}

最佳答案

您将无法在业务逻辑中使用 System.Web。而且,事实上,您不应该这样做。

您可以编写您的 Controller 以接受来自客户端的文件并将其保存在本地服务器中的某个位置。然后,将文件名传递给您的业务逻辑层以进行进一步处理。

你的后续问题:

Q. I am reading the file into a memory stream. Is it advisable to pass the memory stream to the presenter, perform the business logic in the presenter, and finally set the file upload status in view?

是的,这也有效:传递流而不是文件名。

关于c# - 实现文件上传的 MVP 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14368651/

相关文章:

c# - 谷歌货币转换器

c# - 优雅地关闭命名管道并处理流

asp.net - 从 GridView 获取值

c# - 使用C#上传文件到FTP

c# - 如何在不单击的情况下在 FileUpload Controller 中选择路径后显示图像

node.js - 使用 angular-file-upload 将文件上传到 Node 服务器

c# - 如何知道用户更改 winform 中的任何值?

css - UpdateProgress div定位

C# MVC 和 Web API 应用程序架构

c# - Sonarlint 不会将 "Managed Binary Analysis rules"引入 Visual Studio