C# 下载文件 只利用文件的存放路径来下载

第一种方式:
最简单的就是返回一个file类型的数据即FilePathResult类型的对象

             string serverPath = ConfigurationManager.AppSettings["file.disk.path"];
             string name = Path.GetFileName(path);
             return File(serverPath + path, mime, name);

第二种方式:

            string serverPath = ConfigurationManager.AppSettings["file.disk.path"];

            FileInfo file = new FileInfo(serverPath + path);
            if (file.Exists)
            {
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "text/plain";
                Response.TransmitFile(file.FullName);
                Response.End();
            }
posted @ 2017-05-25 17:44  飞鸿踏雪不留痕  阅读(1920)  评论(0编辑  收藏  举报