Exemplo n.º 1
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            //判断用户是否登录
            if (!string.IsNullOrEmpty(hidAccount.Value) && hidAccount.Value != "未登录")
            {
                string         newFile = "";
                string         fileExt = "";
                HttpPostedFile file    = Request.Files[0];
                //判断上传图片的时候img里面有没有图片如果有
                if (string.IsNullOrEmpty(hidImg.Value))
                {
                    //首次上传图片不需要删除原有的

                    if (file.ContentLength > 0)
                    {
                        string fileName = Path.GetFileName(file.FileName);
                        fileExt = Path.GetExtension(fileName);
                        string[] suffix = { ".jpg", ".png" };
                        for (int i = 0; i < suffix.Length; i++)
                        {
                            if (fileExt == suffix[i])
                            {
                                //对上传文件重名命
                                newFile = Guid.NewGuid().ToString();
                                //将上传的文件放在不同的目录下
                                string dir     = "/HeadPortraitImages/" + newFile;
                                string fullDir = dir + fileExt;        //文件存放的完整路劲
                                file.SaveAs(Request.MapPath(fullDir)); //将上传的文件保存在到HeadPortraitImages文件里
                            }
                        }
                    }
                }
                else
                {
                    if (file.ContentLength > 0)
                    {
                        //如果有则删除原有的
                        File.Delete(Server.MapPath("/HeadPortraitImages/" + hidImg.Value));
                        string fileName = Path.GetFileName(file.FileName);
                        fileExt = Path.GetExtension(fileName);
                        string[] suffix = { ".jpg", ".png" };
                        for (int i = 0; i < suffix.Length; i++)
                        {
                            if (fileExt == suffix[i])
                            {
                                //对上传文件重名命
                                newFile = Guid.NewGuid().ToString();
                                //将上传的文件放在不同的目录下
                                string dir     = "/HeadPortraitImages/" + newFile;
                                string fullDir = dir + fileExt;        //文件存放的完整路劲
                                hidImg.Value = fullDir;
                                file.SaveAs(Request.MapPath(fullDir)); //将上传的文件保存在到HeadPortraitImages文件里
                            }
                        }
                    }
                    else
                    {
                        common.MsgTrans("请选择头像", this);
                        return;
                    }
                }
                modelPlayRight = bllPlayRight.GetModel(hidAccount.Value);//得到用户的播放信息
                modelPlayRight.HeadPortrait = newFile + fileExt;
                bllPlayRight.Update(modelPlayRight);
                Response.Redirect("PersonalMessage.aspx");
            }
            else
            {
                common.MsgTrans("请先登录了才能上传头像哦", this);
            }
        }