コード例 #1
0
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (!WebApiHelper.CheckExistenceAccount("api/accountManager/account/exists", 
                    Method.POST, model.UserName, model.Email))
                {
                    User newUser = new User();
                    newUser.UserName = model.UserName;
                    newUser.Password = model.Password;
                    newUser.Email = model.Email;
                    newUser.LastName = model.LastName;
                    newUser.FirstName = model.FirstName;
                    newUser.Phone = model.Phone;

                    if (WebApiHelper.RegisterAccount("api/accountManager/account/register", 
                        Method.POST, new UserApi(newUser)))
                    {
                        ViewBag.Message = "Tài khoản đã được ghi nhận trong hệ thống." 
                            + " Xin vui lòng kiểm tra email để kích hoạt tài khoản.";
                        return View("Result");
                    }
                    else
                    {
                        ViewBag.Alert 
                            = "Chúng tôi không thể gửi email kích hoạt đến email của bạn." 
                            + " Xin lỗi vì sự bất tiện này.";
                        return View("Result");
                    }
                }
                else
                {
                    ViewBag.Message = "Tài khoản đã tồn tại trong hệ thống.";
                    return View("Register");
                }
            }
            return View(model);
        }
コード例 #2
0
        public ActionResult UploadImage(HttpPostedFileBase fileUp)
        {
            User user = new User();
            if (fileUp != null)
            {
                try
                {
                    bool flag = false;
                    /*Lopp for multiple files*/
                    /*Geting the file name*/
                    string fileName = Path.GetFileName(fileUp.FileName);
                    if (fileName != null)
                    {
                        string[] part = fileName.Split('.');
                        if (part[part.Length - 1].ToUpper() != "jpg".ToUpper() &&
                            part[part.Length - 1].ToUpper() != "png".ToUpper() &&
                            part[part.Length - 1].ToUpper() != "bmp".ToUpper())
                        {
                            flag = true;
                        }
                    }

                    if (flag)
                    {
                        ViewBag.Message = "Hình ảnh phải thuộc định dạng *.jpg, *.png hoặc *.bmp";
                    }
                    else
                    {
                        int id = (int)Session["UserId"];
                        user = WebApiHelper.GetUserFromServer("api/accountManager/account/" + id, Method.GET);

                        //fileUp.SaveAs(Server.MapPath("~/Content/images/Avatar/" + fileName));
                        //if (user.UserImage != "avatar.png")
                        //{
                        //    string fullpath = Request.MapPath("~/Content/images/Avatar/" + user.UserImage);
                        //    System.IO.File.Delete(fullpath);
                        //}

                        BinaryReader br = new BinaryReader(fileUp.InputStream);
                        byte[] bs = br.ReadBytes(fileUp.ContentLength);
                        var base64 = Convert.ToBase64String(bs);
                        string img = string.Format("data:{0};base64,{1}", "image/jpg", base64);

                        WebApiHelper.UploadAccountImage("api/accountManager/account/uploadAccountImage", 
                            Method.POST, id, img);

                        ViewBag.Message = "Avatar đã được cập nhật";
                    }
                }
                catch
                {
                    ViewBag.Message = "Có lỗi trong quá trình upload";
                }
            }
            else
            {

                ViewBag.Message = "Vui lòng chọn hình";
            }

            return View(user);
        }