HTML加一般处理程序实现文件上传
2011-08-06 23:16
501 查看
HTML代码:
后台程序:
注:源代码来自传智播客讲师“邹华栋”老师!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <form action="Upload.ashx" method="post" enctype="multipart/form-data"> 选择要上传的图片:<input type="file" name="fileUp" /> <input type="submit" value="上传" /> </form> </body> </html>
后台程序:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.Drawing.Imaging; namespace UploadDemo { /// <summary> /// Summary description for Upload /// </summary> public class Upload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //获得浏览器端 传过来 第一个文件选择框的数据 HttpPostedFile hpFile = context.Request.Files[0]; //要保存的目录路径 string filePath = "upload"; //判断 上传文件数据的长度是否>0 if (hpFile.ContentLength > 0) { //获得上传上来的文件名称 string fileName = System.IO.Path.GetFileName(hpFile.FileName); //获得 要保存的物理路径 filePath = context.Server.MapPath(filePath + "/" + fileName); //将上传来的 文件数据 保存在 对应的 物理路径上 hpFile.SaveAs(filePath); //如果上传上来的是图片文件数据 if (hpFile.ContentType.IndexOf("image") > -1) { //将上传上来的文件对象里的 数据流 转成 图片对象 using (Image img = Image.FromStream(hpFile.InputStream)) { //创建缩略图对象 using (Bitmap thumbImg = new Bitmap(120, 40)) { //创建 【画家】对象,并告诉他要在缩略图上作画 using (Graphics g = Graphics.FromImage(thumbImg)) { //将 原图 img 画在 缩略图 thumbImg上 //第一个长方形参数:要把原图 画成多大 //第二个长方形参数:要画原图的哪个部分(要把原图的哪个部分画到缩略图上) g.DrawImage(img, new Rectangle(0, 0, thumbImg.Width, thumbImg.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); string thumbImgPath = context.Server.MapPath("upload/thumb" + fileName); thumbImg.Save(thumbImgPath); context.Response.Write("***小图成功:" + "/thumb" + fileName); } } } } context.Response.Write("上传成功:" + hpFile.FileName); } else { context.Response.Write("还米有选择要上传的文件!"); } } public bool IsReusable { get { return false; } } } }
注:源代码来自传智播客讲师“邹华栋”老师!
相关文章推荐
- html借助一般处理程序实现文件上传到服务器
- C# 实现 HTML+一般处理程序图片上传功能如图!
- 文件上传(一般处理程序+html)
- 一般处理程序上传文件(html表单上传、aspx页面上传)
- 通过一般处理程序实现【文件上传】
- (转)Asp.Net 用Jquery和一般处理程序实现无刷新上传大文件
- 通过一般处理程序实现【文件上传】
- Asp.Net 用Jquery和一般处理程序实现无刷新上传大文件
- 有html和对应的一般处理程序ashx文件,单击按钮改变文本框的值
- AjaxFileUpLoad 文件异步上传Demo(模仿官方网站,后台使用.net一般处理程序)
- ajaxfileupload.js插件结合一般处理文件实现Ajax无刷新上传
- asp.net使用一般处理程序实现文件下载
- js页面(页面上无服务端控件,且页面不刷新)实现请求一般处理程序下载文件方法
- ASP.NET表单和一般处理程序处理文件上传和下载
- Ajax请求ashx一般处理程序实现文件下载
- java 实现html5多文件选择上传_h5多文件上传_ajax异步多文件上传_java处理多文件上传
- asp.net 一般处理程序接收上传文件的问题
- 图片上传——用一般处理程序实现
- asp.net 上传文件到一般处理程序中
- 文件上传 下载 一般处理程序和webForm联系以及asp.net内置对象