zoukankan      html  css  js  c++  java
  • MVC中简单的文件下载代码

            /// <summary>
            /// 文件下载
            /// </summary>
            /// <param name="filePath"></param>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public FileStreamResult DownLoadFile(string filePath,string fileName)
            {
                Stream stream = null;
                try
                {
                    if (System.IO.File.Exists(filePath))
                    {
                        stream = new FileStream(Server.MapPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        if (stream != null)
                        {
                            return File(stream, "application/octet-stream", Url.Encode(fileName));
                        }
                        else
                        {
                            throw new Exception("文件不存在!");
                        }
                    }
                    else
                    {
                        throw new Exception("文件不存在!");
                    }
                }
                catch(Exception exc)
                {
                    throw new Exception("");
                }
            }
            public void Download()
            {
                Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(Path.GetFileName(Server.UrlDecode(Request["filepath"]))));
                Response.WriteFile(Server.MapPath(Server.UrlDecode(Request["filepath"])));
                Response.Flush();
                Response.End();
            }
  • 相关阅读:
    GO语言网络编程
    GO语言测试
    GO语言反射
    GO语言strconv包的使用
    GO语言并发
    Centos7 开启swap分区
    设计模式 之 命令模式
    设计模式 之 代理模式
    设计模式 之 工厂模式
    设计模式 之 观察者模式
  • 原文地址:https://www.cnblogs.com/wangbogo/p/3075200.html
Copyright © 2011-2022 走看看