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();
            }
  • 相关阅读:
    8.24
    8.23
    今日拔牙牙疼暂时不评论了,明天展示的时候老师也会给出建议
    8.22
    8.21
    8.20
    8.19随笔
    助教培训第四次作业
    助教培训第三次作业-墨刀的练习
    同时安装多个jdk设置及切换
  • 原文地址:https://www.cnblogs.com/wangbogo/p/3075200.html
Copyright © 2011-2022 走看看