zoukankan      html  css  js  c++  java
  • Asp.net mvc3 文件下载的实现

    1.控制器的ActionResult

      public ActionResult DownLoadFile()
            {
                string filePath = Server.MapPath("~/ResourceFile/1ef53469-cbac-4007-b40b-ae68644794e7.doc");
                return new DownloadResult(filePath, "1ef53469-cbac-4007-b40b-ae68644794e7.doc");        
            }

    2.DownLoadResult类的实现

     public class DownloadResult : ActionResult
        {
            public DownloadResult()
            {
            }

            public DownloadResult(string virtualPath,string fileDownloadName)
            {
                this.VirtualPath = virtualPath;
                this.FileDownloadName = fileDownloadName;
            }

            public string VirtualPath
            {
                get;
                set;
            }

            public string FileDownloadName
            {
                get;
                set;
            }

            public override void ExecuteResult(ControllerContext context) {
                  if (!String.IsNullOrEmpty(FileDownloadName)) {
                      context.HttpContext.Response.AddHeader("content-disposition",
                        "attachment; filename=" + this.FileDownloadName);
        }

        string filePath = this.VirtualPath;
        context.HttpContext.Response.TransmitFile(filePath);
        }

        }

  • 相关阅读:
    2016-8-29
    2016-8-25
    2016-8-24
    2016-8-23
    2016-8-22
    2016-8-16
    2016-8-15
    深圳_多测师面试 __腾讯云/_高级讲师肖sir
    深圳_多测师面试 _新字节跳动(2020年10月23日)_高级讲师肖sir
    多测师讲解自动化 _RF_(202)高级讲师肖sir
  • 原文地址:https://www.cnblogs.com/kouhh/p/2471898.html
Copyright © 2011-2022 走看看