Beispiel #1
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            int id;

            if (int.TryParse(context.Request.QueryString["ID"], out id))
            {
                BLL.ImageInfoService imageInfoService = new BLL.ImageInfoService();
                Model.ImageInfo      imageInfo        = new Model.ImageInfo();
                imageInfo = imageInfoService.read(id);
                if (imageInfo != null)
                {
                    string mapPath     = context.Request.MapPath("ImageUpdate.html");
                    string fileContent = File.ReadAllText(mapPath);
                    fileContent = fileContent.Replace("$ID", imageInfo.ID.ToString()).Replace("$OriginalName", imageInfo.OriginalName).Replace("$Path", imageInfo.Path).Replace("$GuidName", imageInfo.GuidName).Replace("$Exetension", imageInfo.Extension).Replace("$ThumbName", imageInfo.ThumbName).Replace("$Size", imageInfo.Size).Replace("$CreateTime", imageInfo.CreateTime.ToString());
                    context.Response.Write(fileContent);
                }
                else
                {
                    context.Response.Write("要更新的图片不存在");
                }
            }
            else
            {
                context.Response.Write("参数错误");
            }
        }
Beispiel #2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";

            string htmlFilePath    = context.Request.MapPath("ImageList.html");
            string htmlFileContent = File.ReadAllText(htmlFilePath);

            BLL.ImageInfoService imageInfoService = new BLL.ImageInfoService();
            List <ImageInfo>     list             = imageInfoService.read();

            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (ImageInfo item in list)
                {
                    sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td><a href='{2}{3}{4}'>{1}</a></td><td><a href='{2}{5}{4}'>thumb</a></td><td><a href='ImageDetail.ashx?ID={0}'>Detail</a></td><td><a href='ImageUpdate.ashx?ID={0}'>Update</a></td><td></td></tr>", item.ID, item.OriginalName, item.Path, item.GuidName, item.Extension, item.ThumbName, item.Size, item.CreateTime);
                }
                htmlFileContent = htmlFileContent.Replace("@toBody", sb.ToString());
            }
            else
            {
                htmlFileContent = htmlFileContent.Replace("@toBody", "");
            }


            context.Response.Write(htmlFileContent);
        }
Beispiel #3
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            HttpPostedFile file = context.Request.Files[0];

            if (file.ContentLength > 0)
            {
                string filePath = Path.GetFileName(file.FileName);  //通过file的完全限定名得出源文件的文件名和扩展名
                string fileExt  = Path.GetExtension(file.FileName); //通过file的完全限定名得出源文件的扩展名
                string guidName;
                string thumbName;
                string imgWidth;
                string imgHeight;
                string dir = "/ImageUpload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
                if (!Directory.Exists(context.Request.MapPath(dir)))
                {
                    Directory.CreateDirectory(context.Request.MapPath(dir));
                }//如果目录不存在就创建一个

                string[] imageExt = { ".jpg", ".png", "bmp", ".jpge" };
                bool     exist    = ((IList)imageExt).Contains(fileExt);

                if (exist)
                {
                    guidName  = Guid.NewGuid().ToString();
                    thumbName = "thumb" + guidName;
                    file.SaveAs(context.Request.MapPath(dir + guidName + fileExt));

                    //画缩略图
                    using (Bitmap map = new Bitmap(100, 100))
                    {
                        using (Graphics g = Graphics.FromImage(map))
                        {
                            Image img = Image.FromStream(file.InputStream);
                            imgWidth  = img.Width.ToString();
                            imgHeight = img.Height.ToString();

                            g.DrawImage(img, 0, 0, 100, 100);
                            map.Save(context.Request.MapPath(dir + thumbName + fileExt), System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                    }

                    //组imageinfo写数据库
                    ImageInfo imageInfo = new ImageInfo();
                    imageInfo.OriginalName = file.FileName;
                    imageInfo.Path         = dir;
                    imageInfo.GuidName     = guidName;
                    imageInfo.Extension    = fileExt;
                    imageInfo.ThumbName    = thumbName;
                    imageInfo.Size         = imgWidth + "X" + imgHeight;
                    imageInfo.CreateTime   = DateTime.Now;

                    BLL.ImageInfoService imageInfoService = new BLL.ImageInfoService();

                    if (imageInfoService.create(imageInfo))
                    {
                        context.Response.Redirect("ImageList.ashx");
                    }
                    else
                    {
                        context.Response.Redirect("/Error.html");
                    }
                }
                else
                {
                    context.Response.Write("仅能上传图片文件");
                }
            }
            else
            {
                context.Response.Write("请选择要上传的图片");
            }
        }