下载文件 下载文件

  1 using Console_Core.BLL;
  2 using Console_Core.Model;
  3 using NPOI.HSSF.UserModel;
  4 using NPOI.SS.UserModel;
  5 using System;
  6 using System.Collections.Generic;
  7 using System.Drawing;
  8 using System.Drawing.Imaging;
  9 using System.IO;
 10 using System.Linq;
 11 using System.Reflection;
 12 using System.Web;
 13 
 14 namespace Web_Cassini
 15 {
 16     /// <summary>
 17     /// download1 的摘要说明
 18     /// </summary>
 19     public class download1 : IHttpHandler
 20     {
 21 
 22         public void ProcessRequest(HttpContext context)
 23         {
 24             context.Response.ContentType = "text/plain";
 25             MyORM_BLL myORM_BLL = new MyORM_BLL();
 26 
 27             #region 默认把当前输出的文件 以附加形式 动态下载 到指定文件中
 28             //默认把当前输出的文件 以附加形式 动态下载 到指定文件中   --AddHeader把表头输出到输出流
 29             context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlDecode("动态文件.txt") + "");
 30             List<object> list = myORM_BLL.SelectAllModel(typeof(TC_STUDENT));
 31             foreach (var obj in list)
 32             {
 33                 TC_STUDENT tc = obj as TC_STUDENT;
 34                 context.Response.Write(tc.USERNAME + "	" + tc.PASSWORD + "	" + tc.GENDER + "	" + tc.PROFESSION);
 35             } 
 36             #endregion
 37 
 38             #region 默认把当前Excel 动态的以附件形式 下载
 39             context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("学生信息.xls"));
 40             List<object> list = myORM_BLL.SelectAllModel(typeof(TC_STUDENT));
 41             //引用NPOI
 42             //创建一个workbook
 43             IWorkbook workbook = new HSSFWorkbook();
 44             //创建sheet
 45             ISheet sheet = workbook.CreateSheet("学生信息");
 46             //遍历List 每一个实例创建一个row
 47             for (int i = 0; i < list.Count; i++)
 48             {
 49                 TC_STUDENT tc = list[i] as TC_STUDENT;
 50                 IRow row = sheet.CreateRow(i);
 51                 Type ty = tc.GetType();
 52                 PropertyInfo[] props = ty.GetProperties();
 53                 //遍历 实例中的列 灭一个列创建一个cell
 54                 for (int j = 0; j < props.Length; j++)
 55                 {
 56                     string propName = props[j].Name;
 57                     object propValue = props[j].GetValue(tc);
 58                     ICell cell = row.CreateCell(j);
 59                     cell.SetCellValue(propValue.ToString());
 60                 }
 61             }
 62             //把workbook写入到输出流,并以附件形式下载
 63             workbook.Write(context.Response.OutputStream); 
 64             #endregion
 65 
 66             #region 动态的 附加形式 下载图片
 67             //动态的 附加形式 下载图片
 68             context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("泡妞证.jpg") + "");
 69             //获得name
 70             string name = context.Request["name"];
 71             //画出图片
 72             using (Bitmap map = new Bitmap(500, 500))
 73             using (Graphics g = Graphics.FromImage(map))
 74             using (Font font1 = new Font(System.Drawing.FontFamily.GenericSerif, 30))
 75             using (Font font2 = new Font(System.Drawing.FontFamily.GenericSerif, 15))
 76             {
 77                 g.DrawString(name, font1, Brushes.Red, 110, 66);
 78                 g.DrawString(name, font2, Brushes.Red, 302, 150);
 79 
 80                 map.Save(context.Response.OutputStream, ImageFormat.Jpeg);
 81             } 
 82             #endregion
 83 
 84             #region 通过提取码 动态下载图片
 85             //获得提取码
 86             string code = context.Request["code"];
 87             //验证 提取码
 88             if (code != "1234")
 89             {
 90                 context.Response.Write("提取码错误");
 91                 return;
 92             }
 93             //获得图片路径 写入流 Copy到输出流
 94             string path = context.Server.MapPath("~/img/我爱你一万年158_038.jpg");
 95             using (Stream stream = File.OpenRead(path))
 96             {
 97                 //html头标记:动态附加形式 下载图片
 98                 context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("我爱你一万年158_038.jpg") + "");
 99                 stream.CopyTo(context.Response.OutputStream);
100             } 
101             #endregion
102         }
103 
104         public bool IsReusable
105         {
106             get
107             {
108                 return false;
109             }
110         }
111     }
112 }
动态 以附件形式 下载图片
  1 using NPOI.HSSF.UserModel;
  2 using NPOI.SS.UserModel;
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Drawing;
  6 using System.Drawing.Imaging;
  7 using System.IO;
  8 using System.Linq;
  9 using System.Text;
 10 using System.Web;
 11 
 12 namespace Web_Cassini
 13 {
 14     /// <summary>
 15     /// upload1 的摘要说明
 16     /// </summary>
 17     public class upload1 : IHttpHandler
 18     {
 19 
 20         public void ProcessRequest(HttpContext context)
 21         {
 22             context.Response.ContentType = "text/html";
 23             #region 上传文件 保存到本地upload
 24             //获得上传的文件
 25             HttpPostedFile file1 = context.Request.Files["file1"];
 26             HttpPostedFile file2 = context.Request.Files["file2"];
 27             //验证 文件大小、类型
 28             if (file1.ContentLength > 1024 * 1024)
 29             {
 30                 context.Response.Write("文件大小不能超过1M");
 31                 return;
 32             }
 33             string fileExt = Path.GetExtension(file1.FileName);
 34             string fileName = Path.GetFileName(file1.FileName);
 35             if (fileExt != ".png" && fileExt != ".jpg" && fileExt != ".gif")
 36             {
 37                 context.Response.Write("只能上传图片");
 38                 return;
 39             }
 40             //把文件保存到本地
 41             string path = context.Server.MapPath("~/upload/") + fileName;
 42             file1.SaveAs(path); 
 43             #endregion
 44 
 45             #region 作业:上传图片 保存到upload/yyyy/mm/dd文件下
 46             //获得上传文件
 47             string title = context.Request["title"]; //自定义的图片标题
 48             HttpPostedFile file1 = context.Request.Files["file1"];
 49             context.Response.Write("<html><head></head><body>"); //起始标签
 50             //验证 大小和后缀
 51             if(file1==null || file1.ContentLength<=0)
 52             {
 53                 context.Response.Write("请先选择文件");
 54                 OutputHtmlEnd(context.Response); //输出结束标签
 55                 return;
 56             }
 57             if (file1.ContentLength > 1024 * 1024)
 58             {
 59                 context.Response.Write("文件不能超过1M");
 60                 OutputHtmlEnd(context.Response); //输出结束标签
 61                 return;
 62             }
 63             string fileExt = Path.GetExtension(file1.FileName);
 64             if (fileExt != ".jpg" && fileExt != ".jpeg" && fileExt != ".gif" && fileExt != ".png")
 65             {
 66                 context.Response.Write("只能上传图片");
 67                 OutputHtmlEnd(context.Response); //输出结束标签
 68                 return;
 69             }
 70             string format = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
 71             string dirFullPath = context.Server.MapPath("~/upload/" + format + "/"); //目录全路径
 72             string fileFullPath =Path.Combine(dirFullPath,file1.FileName); //文件全路径
 73             //画出图片 保存到upload (文件不落地,从流中获得图片)
 74             using (Image img = Bitmap.FromStream(file1.InputStream))
 75             {
 76                 using (Graphics g = Graphics.FromImage(img))
 77                 using (Font font = new Font(System.Drawing.FontFamily.GenericSerif, 20))
 78                 {
 79                     g.DrawString(title, font, Brushes.Red, 0, 0);
 80                 }
 81                 if (!Directory.Exists(dirFullPath))
 82                 {
 83                     Directory.CreateDirectory(dirFullPath);
 84                 }
 85                 img.Save(fileFullPath);
 86                 context.Response.Write("ok");
 87                 OutputHtmlEnd(context.Response);
 88             } 
 89             #endregion
 90 
 91             #region 作业:上传excel文件 web展示内容
 92             //上传excel文件 web展示内容
 93             //获得excel文件
 94             HttpPostedFile file1 = context.Request.Files["file1"];
 95             //验证 非空、大小和后缀
 96             context.Response.Write("<html><head></head><body>"); //起始标签
 97             if (file1 == null || file1.ContentLength <= 0)
 98             {
 99                 context.Response.Write("还未选择文件");
100                 OutputHtmlEnd(context.Response); //html结束标签
101                 return;
102             }
103             if (file1.ContentLength > 1024 * 1024)
104             {
105                 context.Response.Write("文件不能超过1M");
106                 OutputHtmlEnd(context.Response); //html结束标签
107                 return;
108             }
109             string fileExt = Path.GetExtension(file1.FileName);
110             if (fileExt != ".xls" && fileExt != ".xlsx")
111             {
112                 context.Response.Write("只能上传excel文件");
113                 OutputHtmlEnd(context.Response); //html结束标签
114                 return;
115             }
116             //创建一个workbook 拼接html
117             StringBuilder sb = new StringBuilder();
118             IWorkbook workbook = new HSSFWorkbook(file1.InputStream);
119             //IWorkbook workbook = WorkbookFactory.Create(file1.InputStream); //只在2.0中支持
120             for (int i = 0; i < workbook.NumberOfSheets; i++)
121             {
122                 ISheet sheet = workbook.GetSheetAt(i);
123                 context.Response.Write("<h1>" + sheet.SheetName + "</h1>");
124                 sb.AppendLine("<table>");
125                 for (int j = 0; j < sheet.LastRowNum; j++)
126                 {
127                     IRow row = sheet.GetRow(j);
128                     sb.AppendLine("<tr>");
129                     for (int k = 0; k < row.LastCellNum; k++)
130                     {
131                         ICell cell = row.GetCell(k);
132                         string value = cell.StringCellValue;
133                         sb.Append("<td>");
134                         sb.Append(value);
135                         sb.Append("</td>");
136                     }
137                     sb.AppendLine("</tr>");
138                 }
139                 sb.AppendLine("</table>");
140             }
141             context.Response.Write(sb.ToString());
142             OutputHtmlEnd(context.Response);
143             #endregion
144         }
145 
146         /// <summary>
147         /// 输出html结束标签
148         /// </summary>
149         /// <param name="httpResponse"></param>
150         private void OutputHtmlEnd(HttpResponse httpResponse)
151         {
152             httpResponse.Write("</body></html>");
153         }
154 
155 
156         public bool IsReusable
157         {
158             get
159             {
160                 return false;
161             }
162         }
163     }
164 }
上传文件
原文地址:https://www.cnblogs.com/adolphyang/p/4770352.html