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

        }

  • 相关阅读:
    meta 标签禁止缩放失效
    [UE4]打包EXE
    [UE4]Set Array Elem
    [UML]用例图
    [UE4]函数参数引用
    阻止移动鼠标双击页面放大, no double tap
    spring boot入门 -- 介绍和第一个例子
    SpringBoot 启动错误搜集
    spring boot 启动找不到或无法加载主类
    Spring Boot中Starter是什么
  • 原文地址:https://www.cnblogs.com/kouhh/p/2471898.html
Copyright © 2011-2022 走看看