コード例 #1
0
        protected void CreatAlbum_OnClick(object sebder, EventArgs e)
        {
            int groupId = 0;
            CY.UME.Core.Business.Group group;

            #region validate
            if (TBXAlbumName.Text.Trim().Length == 0)
            {
                ShowAlert("提示", "相册名不能为空");
                return;
            }

            if (HF_groupId.Value != "" && int.TryParse(HF_groupId.Value.ToString(), out groupId))
            {
                group = CY.UME.Core.Business.Group.Load(groupId);

                if (group == null)
                {
                    throw new Exception("该群组不存在或已被删除");
                }
            }
            else
            {
                throw new Exception("参数错误");
            }
            #endregion

            #region creat album
            int viewPermission = 1;
            int.TryParse(selViewPermission.Value, out viewPermission);
            long albumId = 0;

            CY.UME.Core.Business.Album album = new CY.UME.Core.Business.Album();

            if (HF_albumId.Value != "" && long.TryParse(HF_albumId.Value.ToString(), out albumId))
            {
                album = CY.UME.Core.Business.Album.Load(albumId);

                if (album == null)
                {
                    album = group.CreateGroupAlbum(TBXAlbumName.Text.Trim(), viewPermission);

                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>cy.ume.ui.window({ title: '提示', content: '创建相册成功' });window.location.href='PictureList.aspx?albumId=" + album.Id + "'</script>");
                }
                else
                {
                    album.Name = TBXAlbumName.Text.Trim();
                    album.ViewPermission = int.Parse(selViewPermission.Value);
                    album.Save();

                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>cy.ume.ui.window({ title: '提示', content: '修改相册成功' });window.location.href='PictureList.aspx?albumId=" + album.Id + "'</script>");
                }
            }
            else
            {
                album = group.CreateGroupAlbum(TBXAlbumName.Text.Trim(), viewPermission);

                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>cy.ume.ui.window({ title: '提示', content: '创建相册成功' });window.location.href='PictureList.aspx?albumId=" + album.Id + "'</script>");
            }
            #endregion

            #region upload pic

            string strPicId = String.Empty;
            List<HttpPostedFile> hfList = new List<HttpPostedFile>();
            if (PortugueseFileUpload1.HasFile)
            {
                hfList.Add(PortugueseFileUpload1.PostedFile);
            }
            if (PortugueseFileUpload2.HasFile)
            {
                hfList.Add(PortugueseFileUpload2.PostedFile);
            }
            if (PortugueseFileUpload3.HasFile)
            {
                hfList.Add(PortugueseFileUpload3.PostedFile);
            }
            if (PortugueseFileUpload4.HasFile)
            {
                hfList.Add(PortugueseFileUpload4.PostedFile);
            }
            if (PortugueseFileUpload5.HasFile)
            {
                hfList.Add(PortugueseFileUpload5.PostedFile);
            }

            for (int i = 0; i < hfList.Count; i++)
            {
                HttpPostedFile imgFile = hfList[i];
                if (imgFile == null || imgFile.ContentLength == 0)
                {
                    break;
                }
                string imgExtention = CY.Utility.Common.FileUtility.GetFileExtension(imgFile.FileName).ToLower();
                string imgName = CY.Utility.Common.FileUtility.GetFileName(imgFile.FileName);

                string appPath = CY.Utility.Common.RequestUtility.CurPhysicalApplicationPath;
                string dirPath = appPath + "/Content/Group/Album/" + groupId + "/";
                string fileName = "/Content/Group/Album/" + groupId + "/" + DateTime.Now.ToString("yyyMMddhhmmss") + DateTime.Now.Millisecond.ToString();//相册图片以加时间命名

                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dirPath);
                if (!di.Exists)
                {
                    di.Create();
                }

                string bImgName = appPath + fileName + "_big" + imgExtention;
                string mImgName = appPath + fileName + "_middle" + imgExtention;
                string sImgName = appPath + fileName + "_small" + imgExtention;

                imgFile.SaveAs(appPath + fileName + imgExtention);//临时保存原始图片

                Bitmap bmp = new Bitmap(appPath + fileName + imgExtention);
                int width = bmp.Width;
                int height = bmp.Height;

                bmp.Dispose();

                if (height <= width && width >= 600)
                {
                    height = Convert.ToInt32(width > 600 ? (600f / width) * height : height);
                    width = 600;
                }
                else if (width < height && height > 600)
                {
                    width = Convert.ToInt32(height > 600 ? (600f / height) * width : height);
                    height = 600;
                }

                // 将原始图压缩为大缩略图
                using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, bImgName, width, height);
                }

                if (width > 200)
                {
                    height = Convert.ToInt32(width > 200 ? (200f / width) * height : height);
                    width = 200;
                }

                // 生成图片(好友上传图片最新通知中显示)
                using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, sImgName, width, height);
                }

                if (height <= width && width >= 100)
                {
                    height = Convert.ToInt32(width > 100 ? (100f / width) * height : height);
                    width = 100;
                }
                else if (width < height && height > 100)
                {
                    width = Convert.ToInt32(height > 100 ? (100f / height) * width : height);
                    height = 100;
                }

                // 将大图片压缩为中等缩略图
                using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, mImgName, width, height);
                }

                try
                {
                    File.Delete(appPath + fileName + imgExtention);
                }
                catch
                {
                    ;
                }

                //更换相册封面
                if (album.PhotoCount == 0 || album.CoverPath.Trim().Length == 0)
                {
                    album.CoverPath = fileName + "_middle" + imgExtention;
                    album.Save();
                }

                //将路径及图片信息保存到数据库
                CY.UME.Core.Business.Picture pic = new CY.UME.Core.Business.Picture();

                pic.AlbumId = album.Id;
                pic.BigPath = fileName + "_big" + imgExtention;
                pic.DateCreated = DateTime.Now;
                pic.MiddlePath = fileName + "_middle" + imgExtention;
                pic.Name = CY.Utility.Common.StringUtility.HTMLEncode(imgName.Substring(0, imgName.Length > 30 ? 30 : imgName.Length));
                pic.SmallPath = fileName + "_small" + imgExtention;

                pic.Save();

                strPicId += pic.Id.ToString() + ",";
                //保存pictureextend
                CY.UME.Core.Business.PictureExtend pe = new CY.UME.Core.Business.PictureExtend();

                pe.Id = pic.Id;
                pe.AccountId = CurrentAccount.Id;
                pe.ActivityId = "group";
                pe.ActivityPicId = "0";

                pe.Save();
            }

            if (viewPermission == 0 && strPicId.Length > 0)
            {
                IList<CY.UME.Core.Business.Friendship> fsList = CurrentAccount.GetFriendships();

                foreach (CY.UME.Core.Business.Friendship fs in fsList)
                {
                    CY.UME.Core.Business.Notice n = new CY.UME.Core.Business.Notice();

                    n.AccountId = fs.FriendId;
                    n.AuthorId = fs.AccountId;
                    n.Content = "上传了新照片";
                    n.DateCreated = DateTime.Now;
                    n.InstanceId = strPicId.Remove(strPicId.Length - 1);
                    n.IsReaded = false;
                    n.Type = "grouppicture";

                    n.Save();
                }
            }
            #endregion
        }
コード例 #2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            HttpPostedFile imgFile;
            int activeID = 0;
            long accountId = 0;

            if (!int.TryParse(context.Request.QueryString["activeID"].ToString(), out activeID)
                || !long.TryParse(context.Request.QueryString["accountId"].ToString(), out accountId))
            {
                return;
            }

            CY.UME.Core.Business.Activities active = CY.UME.Core.Business.Activities.Load(activeID);

            if (active == null)
            {
                return;
            }

            CY.UME.Core.Business.Account account = CY.UME.Core.Business.Account.Load(accountId);
            if (account == null || account.Id == 0)
            {
                return;
            }

            CY.UME.Core.Business.Album album = active.GetActiveAlbum();

            if (album == null || album.Id == 0)
            {
                album = active.CreateActiveAlbum();
            }

            //if (accountId != active.Sponsor)
            //{
            //    context.Response.Write("只有活动发起人才能上传图片");
            //    return;
            //}
            //else
            //{
            //获取上传的文件并保存
            if (context.Request.Files.Count > 0)
            {
                for (int i = 0; i < context.Request.Files.Count; i++)
                {
                    imgFile = context.Request.Files[i];

                    if (imgFile == null)
                    {
                        continue;
                    }

                    string imgExtention = CY.Utility.Common.FileUtility.GetFileExtension(imgFile.FileName).ToLower();
                    string imgName = CY.Utility.Common.FileUtility.GetFileName(imgFile.FileName);

                    string appPath = CY.Utility.Common.RequestUtility.CurPhysicalApplicationPath;
                    string dirPath = appPath + "/Content/Activites/" + accountId + "/";
                    string fileName = "/Content/Activites/" + accountId + "/" + DateTime.Now.ToString("yyyMMddhhmmss") + DateTime.Now.Millisecond.ToString();//相册图片以加时间命名

                    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dirPath);
                    if (!di.Exists)
                    {
                        di.Create();
                    }

                    string bImgName = appPath + fileName + "_big" + imgExtention;
                    string mImgName = appPath + fileName + "_middle" + imgExtention;
                    string sImgName = appPath + fileName + "_small" + imgExtention;

                    imgFile.SaveAs(appPath + fileName + imgExtention);//临时保存原始图片

                    Bitmap bmp = new Bitmap(appPath + fileName + imgExtention);
                    int width = bmp.Width;
                    int height = bmp.Height;

                    bmp.Dispose();

                    if (height <= width && width >= 600)
                    {
                        height = Convert.ToInt32(width > 600 ? (600f / width) * height : height);
                        width = 600;
                    }
                    else if (width < height && height > 600)
                    {
                        width = Convert.ToInt32(height > 600 ? (600f / height) * width : height);
                        height = 600;
                    }

                    // 将原始图压缩为大缩略图
                    using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                    {
                        CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, bImgName, width, height);
                    }

                    if (width > 200)
                    {
                        height = Convert.ToInt32(width > 200 ? (200f / width) * height : height);
                        width = 200;
                    }

                    // 生成图片(好友上传图片最新通知中显示)
                    using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                    {
                        CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, sImgName, width, height);
                    }

                    if (height <= width && width >= 100)
                    {
                        height = Convert.ToInt32(width > 100 ? (100f / width) * height : height);
                        width = 100;
                    }
                    else if (width < height && height > 100)
                    {
                        width = Convert.ToInt32(height > 100 ? (100f / height) * width : height);
                        height = 100;
                    }

                    // 将大图片压缩为中等缩略图
                    using (StreamReader reader = new StreamReader(appPath + fileName + imgExtention))
                    {
                        CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, mImgName, width, height);
                    }

                    try
                    {
                        File.Delete(appPath + fileName + imgExtention);
                    }
                    catch
                    {
                        ;
                    }

                    //将路径及图片信息保存到数据库
                    CY.UME.Core.Business.Picture pic = new CY.UME.Core.Business.Picture();

                    pic.AlbumId = album.Id;
                    pic.BigPath = fileName + "_big" + imgExtention;
                    pic.DateCreated = DateTime.Now;
                    pic.MiddlePath = fileName + "_middle" + imgExtention;
                    pic.Name = CY.Utility.Common.StringUtility.HTMLEncode(imgName.Substring(0, imgName.Length > 30 ? 30 : imgName.Length));
                    pic.SmallPath = fileName + "_small" + imgExtention;

                    pic.Save();

                    //保存pictureextend
                    CY.UME.Core.Business.PictureExtend pe = new CY.UME.Core.Business.PictureExtend();

                    pe.Id = pic.Id;
                    pe.AccountId = account.Id;
                    pe.ActivityId = "active";
                    pe.ActivityPicId = "0";

                    pe.Save();

                    context.Response.Write("1");
                }
            }
        }