首页 > Web开发 > 详细

简单的上传复用代码

时间:2015-05-10 16:57:28      阅读:314      评论:0      收藏:0      [点我收藏+]
 try
            {
                //得到图片的名称
                string name = System.IO.Path.GetFileNameWithoutExtension(imgSrc);
                //得到图片的后缀名
                string extName = System.IO.Path.GetExtension(imgSrc);
                string Name = System.IO.Path.GetFileName(imgSrc);
                string s = Server.MapPath("/" + Name);
                //生成一个新的名称
                string newName = Guid.NewGuid() + extName;
                //得到图片文件夹所在的物理路径
                string oldRelative = "/Content/Images/HeadIMG/old/"; 
                string oldPath = Server.MapPath(oldRelative);
                string thumRelative = "/Content/Images/HeadIMG/thum/";
                string thumPath = Server.MapPath(thumRelative);
 
                FileStream fs = null;
                fs = new FileStream(imgSrc, FileMode.Open);
                using (System.Drawing.Image img = System.Drawing.Image.FromStream(fs))
                {
                    //保存原图
                    img.Save(oldPath + newName);
                    //得到缩略图
                    System.Drawing.Image thumImg = img.GetThumbnailImage(20, 20, null, System.IntPtr.Zero);
                    thumImg.Save(thumPath + newName);
                }
 
                //储存压缩图片路径
                HttpCookie imgcook = new HttpCookie("ImgThumPath", thumRelative + newName);
                imgcook.Expires = DateTime.Now.AddDays(3);
                Response.Cookies.Add(imgcook);
                return Json(0, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(1, JsonRequestBehavior.AllowGet);
                throw;
            }

简单的上传复用代码

原文:http://www.cnblogs.com/gx1069/p/4492392.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!