为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

无刷新上传

2013-04-25 7页 doc 51KB 13阅读

用户头像

is_198502

暂无简介

举报
无刷新上传引用到的js ----ajaxfileupload.js function ajaxFileUpload(filename) { $.ajaxFileUpload ( { url: '[color=#339966]imageFile.ashx[/color]? param=uploadfile&filename='+filename, ...
无刷新上传
引用到的js ----ajaxfileupload.js 页面 imageFile.ashx文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.IO; using System.Drawing; namespace AjaxLibrary { public class imageFile:IHttpHandler { bool IHttpHandler.IsReusable { get { return false; } } void IHttpHandler.ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html";//设置输出的类型 *** string param=context.Request.QueryString["param"];//类型 string filenames = context.Request.QueryString["filename"]; string error = ""; string msg = ""; string filePath = ""; switch (param) { case "uploadfile": bool boolen = false; HttpPostedFile postFile = context.Request.Files [filenames];//httpPostFile if (postFile.ContentLength == 0) { error = "无效的文件"; } else { string mapPathDir="~/upload_files/"; if(System.IO.Directory.Exists (context.Server.MapPath(mapPathDir))!=true) { System.IO.Directory.CreateDirectory (context.Server.MapPath(mapPathDir)); } UploadFile(context, postFile, mapPathDir,boolen,out error,out msg,out filePath); } string result = "{error:'"+error+"',msg:'"+msg+"',filepath:'"+filePath+"'}"; context.Response.Output.Write(result); break; case "delfile": string RequestFilePath = context.Request.QueryString ["filepath"];//获取要删除的图片的路径() 如:upload_files/XXXXYYY.gif DelImgFile(context, RequestFilePath, out error, out msg); string delresult = "{ error:'" + error + "', msg:'" + msg + "'}"; context.Response.Output.Write(delresult); break; } } void UploadFile(HttpContext context,HttpPostedFile httppostFile,string paths,bool boolBenl,out string err,out string msg,out string filepath) { string fileName =Path.GetExtension (httppostFile.FileName).ToLower(); string firstName = DateTime.Now.ToString("yyyyMMddHHmmss"); string ThumbName = "Thumb_" + DateTime.Now.ToString ("yyyyMMddHHmmss"); string newNamePath =paths+firstName + fileName; string ThumbNamePath =context.Server.MapPath(paths + ThumbName + fileName); string mapPathStr = context.Server.MapPath(newNamePath); httppostFile.SaveAs(mapPathStr); if (boolBenl == true) { CoverDoubleImage(httppostFile,202,200,ThumbNamePath); } err = ""; msg = "上传成功"; filepath = firstName + fileName; } //删除目录文件 void DelImgFile(HttpContext context, string imgName,out string error,out string msg) { if (imgName.Length != 0 && imgName.IndexOf('/')>-1) { string paths = context.Server.MapPath(imgName); string Thumbpaths = context.Server.MapPath (imgName.Split('/')[0] + "/Thumb_" + imgName.Split('/')[1]); try { if (System.IO.File.Exists(paths) == true) File.Delete(paths); if (System.IO.File.Exists(Thumbpaths) == true) File.Delete(Thumbpaths); } catch (Exception ee) { error = ee.Message; } error = ""; msg = "删除成功"; } else { error = ""; msg = "无效的请求"; } } #region 上传图片并自动生成缩略图JPG /// /// 上传图片并自动生成缩略图JPG /// /// FileUpload控件引用 /// 缩略图宽度 /// 缩略图高度 /// 缩略图路径 public static void CoverDoubleImage(HttpPostedFile FileUpload1, int tWidth, int tHeight, string savePath) { //检查上传文件的格式是否有效 if (FileUpload1.ContentType.ToLower().IndexOf("image") < 0) { // Use.JS.Alert("图片格式错误"); return; } //生成原图 System.IO.Stream oStream = FileUpload1.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream (oStream); int oWidth = oImage.Width; //原图宽度 int oHeight = oImage.Height; //原图高度 //生成缩略原图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度 呈现平滑程度 g.Clear(Color.Transparent); //清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); try { //以JPG格式保存图片 //oImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); //保存原图 tImage.Save (savePath,System.Drawing.Imaging.ImageFormat.Jpeg); //保存缩略图 } catch (Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } } #endregion } }              
/
本文档为【无刷新上传】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索