示例#1
0
        public async Task <ActionResult> Create([Bind(Include = "id,UPhone,UPass,UAdress,Img,Roll_id,Pack_id,Exp_Date,AccNum")] Users users, HttpPostedFileBase Url)
        {
            string[] formats = new string[] { ".jpg", ".png", ".gif", ".jpeg" };
            ViewBag.Pack_id = new SelectList(db.Packs, "id", "name", users.Pack_id);
            string url_img = "";

            if (ModelState.IsValid)
            {
                if (Url != null)
                {
                    string path = "";
                    if (db.Users.Where(p => p.UPhone == users.UPhone).FirstOrDefault() != null)
                    {
                        ViewBag.ExErr = "This phone number has been registered before";
                        return(View(users));
                    }
                    try
                    {
                        path     = Path.Combine(Server.MapPath("~/images"), Path.GetFileName(Url.FileName));
                        url_img += Path.GetFileName(Url.FileName) + ",";
                    }
                    catch (Exception e)
                    {
                        ViewBag.FileStatus = "Error while file uploading.";
                    }
                    string ex = Path.GetExtension(Url.FileName);
                    if (!r.check(ex.ToLower(), formats))
                    {
                        ViewBag.FileStatus = ex + " is not an image";
                        return(View(users));
                    }
                    users.Img = url_img.Substring(0, url_img.Length - 1);
                    if (users.Pack_id == 1)
                    {
                        users.Exp_Date = DateTime.Now.AddMonths(1);
                    }
                    else if (users.Pack_id == 2)
                    {
                        users.Exp_Date = DateTime.Now.AddYears(1);
                    }
                    else
                    {
                        users.Exp_Date = DateTime.Now;
                    }
                    Url.SaveAs(path);
                    string hashed = r.HashPwd(users.UPass);
                    users.UPass = hashed;
                    //users.Roll_id = 2;
                    db.Users.Add(users);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            ViewBag.Roll_id = new SelectList(db.Roles, "id", "name", users.Roll_id);
            return(View(users));
        }
示例#2
0
        public ActionResult ChangePhoto(HttpPostedFileBase Url)
        {
            string[] formats = new string[] { ".jpg", ".png", ".gif", ".jpeg" };
            string   url_img = "";
            var      users   = (Users)Session["user"];

            if (users != null)
            {
                if (Url != null)
                {
                    try
                    {
                        string path = Path.Combine(Server.MapPath("~/images"), Path.GetFileName(Url.FileName));
                        Url.SaveAs(path);
                        url_img += Path.GetFileName(Url.FileName) + ",";
                    }
                    catch (Exception e)
                    {
                        ViewBag.FileStatus = "Error while file uploading.";
                    }
                    string ex = Path.GetExtension(Url.FileName);
                    if (!r.check(ex.ToLower(), formats))
                    {
                        ViewBag.FileStatus = ex + " is not an image";
                        return(View());
                    }
                    users.Img = url_img.Substring(0, url_img.Length - 1);
                    var u = db.Users.Find(users.id);
                    u.Img = users.Img;
                    db.SaveChanges();
                    Session["user"] = users;
                    return(RedirectToAction("Edit/" + users.id));
                }
                else
                {
                    ViewBag.FileStatus = "You must upload an image";
                    return(View());
                }
            }
            else
            {
                return(RedirectToAction("LoginView"));
            }
        }