Пример #1
0
        private string SaveImage(HttpPostedFileBase newsImage, int newsID)
        {
            if (newsImage == null || newsID == 0)
                return null;
            string fileName;
            string fileExtension = newsImage.FileName.Substring(newsImage.FileName.IndexOf("."));
            string savePath = StringUtility.GetSavePath(newsID, out fileName);
            string dirPath = AppDomain.CurrentDomain.BaseDirectory + newsImageFolder + savePath;
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);

            ImagePlus imagePlus = new ImagePlus(newsImage.InputStream);
            imagePlus.MakeThumbnail(280, 205, 1);
            savePath = newsImageFolder + savePath + fileName + fileExtension;
            imagePlus.SaveAs(AppDomain.CurrentDomain.BaseDirectory + savePath);
            return savePath;
        }
Пример #2
0
        public ActionResult UploadImage(int groupId)
        {
            if (Request.Files != null && Request.Files.Count > 0)
            {
                Picture picture = null;
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase imageFile = Request.Files[i];

                    if (imageFile.ContentLength <= 0)
                        continue;
                    picture = new Picture();
                    picture.GroupID = groupId;
                    picture.OrderInGroup = 0;
                    picture.AddTime = DateTime.Now;
                    BizPicture.AddPicture(picture);

                    try
                    {
                        string imageName = null;
                        string dir = StringUtility.GetSavePath(picture.PictureID, out imageName);

                        picture.LocalPath = dir + imageName + "_o" + Path.GetExtension(imageFile.FileName);
                        picture.ThumbnailPath = dir + imageName + "_t" + Path.GetExtension(imageFile.FileName);
                        BizPicture.Update(
                            picture.PictureID,
                            pic => new Picture
                            {
                                LocalPath = picture.LocalPath,
                                ThumbnailPath = picture.ThumbnailPath
                            }
                        );
                        string folder = Settings.Default.UploadFolder.Substring(1);
                        string str = string.Format("{0}{1}{2}", AppDomain.CurrentDomain.BaseDirectory, folder, dir);
                        Directory.CreateDirectory(str);
                        imageFile.SaveAs(string.Format("{0}{1}{2}", AppDomain.CurrentDomain.BaseDirectory, folder, picture.LocalPath));

                        ImagePlus imagePlus = new ImagePlus(imageFile.InputStream);
                        imagePlus.MakeThumbnail(160, 120,2);
                        imagePlus.SaveAs(string.Format("{0}{1}{2}", AppDomain.CurrentDomain.BaseDirectory, folder, picture.ThumbnailPath));
                    }
                    catch
                    {
                        BizPicture.Delete(picture.PictureID);
                        throw;
                    }
                }
            }
            return Content("1");
        }