码迷,mamicode.com
首页 > Web开发 > 详细

简单的上传复用代码

时间:2015-05-10 16:58:39      阅读:133      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!