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);
        }

        }

  • 相关阅读:
    后端Golang+前端React架构开发案例
    Vim技巧大全
    Draggable Modal dialog in Bootstrap
    Github.com的镜像站
    kettle之excel上传数据库
    自定义函数之分割函数
    jmeter 压测 ActiveMq 消息队列
    SQL---查找+删除重复记录
    异常值检测(Outlier Detection)
    使用u盘在pc上安装centos7(安装停留在dracut:/#的处理)
  • 原文地址:https://www.cnblogs.com/kouhh/p/2471898.html
Copyright © 2011-2022 走看看