博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net mvc文件下载
阅读量:7224 次
发布时间:2019-06-29

本文共 4483 字,大约阅读时间需要 14 分钟。

一、zip打包下载

1.依赖引用:ICSharpCode.SharpZipLib

2.设定网站有单独文件服务器,网站目录下有虚拟路径FileFolder,通过虚拟路径将文件映射到文件服务器。

设定根据Guid id可以获取到所需的文件集合

///         /// 下载zip文件        ///         ///         public void DownloadFiles(Guid id)        {            var files = fileservice.GetFiles(c => c.GroupId == id).ToList();            List
paths = new List
(files.Count); //数据库文件存储示例:\FileFolder\file/20161215170146_9767.pdf string savePath = HttpContext.Server.MapPath(@"~\FileFolder\"); foreach (var file in files) { //验证文件是否存在... paths.Add(savePath + file.FilePath.Replace(@"\FileFolder\", "")); } string downloadName = "批量下载" + files[0].FileName + "等"; HttpContext.Response.Clear(); HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + downloadName + ".zip"); HttpContext.Response.ContentType = "application/zip"; HttpContext.Response.CacheControl = "Private"; HttpContext.Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); DownloadZipToBrowser(paths); }
///         /// 下载压缩文件        ///         ///         private void DownloadZipToBrowser(IEnumerable
list) { ZipOutputStream zipOutputStream = null; var response = HttpContext.Response; try { byte[] buffer = new byte[4096]; zipOutputStream = new ZipOutputStream(response.OutputStream); zipOutputStream.SetLevel(6); //0-9, 9 being the highest level of compression foreach (string fileName in list) { string filepath = Server.MapPath(fileName); Stream fs = System.IO.File.OpenRead(filepath); ZipEntry entry = new ZipEntry(Path.GetFileName(filepath)); entry.Size = fs.Length; zipOutputStream.PutNextEntry(entry); int count = fs.Read(buffer, 0, buffer.Length); while (count > 0) { zipOutputStream.Write(buffer, 0, count); count = fs.Read(buffer, 0, buffer.Length); if (!response.IsClientConnected) { break; } response.Flush(); } fs.Close(); } } catch (Exception) { } finally { if (zipOutputStream != null) zipOutputStream.Close(); response.Flush(); response.End(); } }

二、直接下载某一个文件:

///         /// 下载文件        ///         /// FileId        public void DownloadFile(int id )        {            string savePath = HttpContext.Server.MapPath(@"~\FileFolder\");            var fileinfo = fileservice.Find(id);            string fullname = savePath + fileinfo.FilePath.Replace(@"\FileFolder\", "");            //string fullname = fileinfo.FilePath;            //判断文件是否存在            if (!System.IO.File.Exists(fullname))            {                Response.Write("该文件不存在服务器上");                Response.End();                return;            }            FileInfo fi = new FileInfo(fullname);            //**********处理可以解决文件类型问题            string fileextname = fi.Extension;            string DEFAULT_CONTENT_TYPE = "application/unknown";            RegistryKey regkey, fileextkey;            string filecontenttype;            try            {                regkey = Registry.ClassesRoot;                fileextkey = regkey.OpenSubKey(fileextname);                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();            }            catch            {                filecontenttype = DEFAULT_CONTENT_TYPE;            }            //**********end            Response.Clear();            Response.Charset = "utf-8";            Response.ContentEncoding = System.Text.Encoding.UTF8;            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileinfo.FileName, System.Text.Encoding.UTF8));            Response.ContentType = filecontenttype;            Response.WriteFile(fullname);            Response.End();        }

 

 

转载于:https://www.cnblogs.com/riddly/p/7977750.html

你可能感兴趣的文章
ucore操作系统实验笔记 - 重新理解中断
查看>>
leetcode46 Permutation 排列组合
查看>>
Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
查看>>
从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
查看>>
JS题目及答案整理
查看>>
C++11: atomic 头文件
查看>>
听说你叫Java(二)–Servlet请求
查看>>
vue脚手架vue-cli
查看>>
算法---两个栈实现一个队列
查看>>
redis学习笔记(三):列表、集合、有序集合
查看>>
TypeScript迭代器
查看>>
python学习笔记 - ThreadLocal
查看>>
基于组件的设计工作流与界面抽象
查看>>
案例分享〡三拾众筹持续交付开发流程支撑创新业务
查看>>
vagrant 添加本地 box 安装 laravel homestead
查看>>
Vim 折腾记
查看>>
eclipse(luna)创建web工程
查看>>
简洁Java之道
查看>>
QCon讲师对对碰——洪小军采访梁宇鹏:就是爱Golang
查看>>
Python数据可视化的10种技能
查看>>