zoukankan      html  css  js  c++  java
  • mvc 下载示例(from AllInOne Code Framework)

      本例实现了浏览服务器端文件和选中文件列表中某一文件进行下载。

      FileController:

      

    代码
    public class FileController : Controller
        {
            
    // Action for list all the files in "~/App_Data/download" directory
            public ActionResult List()
            {

                
    // Retrive file list
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/App_Data/download/"));

                
    // Filter it via LINQ to Object
                var files = from f in dir.GetFiles("*.*", SearchOption.TopDirectoryOnly)
                            
    where f.Extension != "exe"
                            select f;
                
                
    // Call the corresponding View
                return View(files.ToList());
            }

            
    // Action for return the binary stream of a specified file
            public ActionResult Download(string fn)
            {
                
    // Check whether requested file is valid
                string pfn = Server.MapPath("~/App_Data/download/" + fn);
                
    if (!System.IO.File.Exists(pfn))
                {
                    
    throw new ArgumentException("Invalid file name or file not exists!");
                }

                
    // Use BinaryContentResult to encapsulate the filecontent and return it
                return new BinaryContentResult()
                {
                    FileName 
    = fn,
                    ContentType 
    = "application/octet-stream",
                    Content 
    = System.IO.File.ReadAllBytes(pfn)
                };

            }
        }

      BinaryContentResult 类定义:

      

    代码
        public class BinaryContentResult : ActionResult
        {
            
    // Default constructor
            public BinaryContentResult()
            {
            }

            
    // Properties for encapsulating http headers
            public string ContentType { getset; }
            
    public string FileName { getset; }
            
    public byte[] Content { getset; }

            
    // Underlying code which set the httpheaders and output file content
            public override void ExecuteResult(ControllerContext context)
            {

                context.HttpContext.Response.ClearContent();
                context.HttpContext.Response.ContentType 
    = ContentType;

                context.HttpContext.Response.AddHeader(
    "content-disposition",

                
    "attachment; filename=" + FileName);

                context.HttpContext.Response.BinaryWrite(Content);
                context.HttpContext.Response.End();
            }
        }

      由上面的定义我们可以看到,我们可以重载不同的ActionResult。ActionResult类定义如下:

    代码
    namespace System.Web.Mvc
    {
        
    // 摘要:
        
    //     Encapsulates the result of an action method and is used to perform a framework-level
        
    //     operation on the action method's behalf.
        public abstract class ActionResult
        {
            
    protected ActionResult();

            
    // 摘要:
            
    //     Enables processing of the result of an action method by a custom type that
            
    //     inherits from System.Web.Mvc.ActionResult.
            
    //
            
    // 参数:
            
    //   context:
            
    //     The context within which the result is executed.
            public abstract void ExecuteResult(ControllerContext context);
        }
    }
      我感觉这个示例比较好,思路很清晰。此示例对我感觉最深的是提供一个mvc下载文件的解决方案,还有我们可以自己定义一个能满足自己需要的ActionResult类。
  • 相关阅读:
    re模块的split(),sub()方法 新发现
    tf.where()&np.random.RandomState.rand()&np.vstack&np.mgrid .ravel np.c_[]
    Embedding实现1pre1
    tf.keras训练iris数据集
    tf.keras 搭建神经网络六部法
    循环计算过程(4pre1)
    池化(pooling)舍弃(dropout)& 卷积神经网络
    [C]gcc编译器的一些常用语法
    [POSIX]文件系统(概述)
    [Linux]PHP-FPM与NGINX的两种通讯方式
  • 原文地址:https://www.cnblogs.com/BlueWoods/p/1883890.html
Copyright © 2011-2022 走看看