Example #1
0
        public async Task <JsonResult> addItem(Item item, HttpPostedFileBase[] images)
        {
            ds     = item.addItem();
            jsonRs = JsonConvert.SerializeObject(ds, Formatting.Indented);
            int itemId = (int)ds.Tables[0].Rows[0]["itemId"];

            if (itemId > 0)
            {
                string errorImg  = "";
                string thumbnail = "";
                for (int i = 0; i < images.Length; i++)
                {
                    try
                    {
                        string extension = Path.GetExtension(images[i].FileName);
                        string imageName = string.Format(Constant.itemImageNameFormat, itemId, i + 1, extension);
                        string avatarUrl = await AzureBlobController.UploadImageAsync(images[i], imageName);

                        if (avatarUrl != null)
                        {
                            thumbnail += avatarUrl;
                            if (i != images.Length)
                            {
                                thumbnail += ", ";
                            }
                        }
                        else
                        {
                            errorImg += i.ToString() + ", ";
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("(Error - store:  " + storeName + ")Exception: ", ex);
                    }
                }
                if (errorImg.Length >= 2)
                {
                    errorImg = errorImg.Substring(0, errorImg.Length - 2);
                }
                if (thumbnail.Length > 0)
                {
                    thumbnail = thumbnail.Substring(0, thumbnail.Length - 2);
                    ds        = Item.setItemThumbnail(itemId, thumbnail);
                    jsonRs    = "{\r\n  \"Table\": [\r\n      {\r\n      \"itemId\": " + itemId + "}\r\n  ]\r\n}";
                }
                else
                {
                    jsonRs = "{\r\n  \"Table\": [\r\n      {\r\n      \"error\": \"Upload hình ảnh thất bại ở vị trí" + errorImg + "\"}\r\n  ]\r\n}";
                }
            }
            else
            {
                jsonRs = "{\r\n  \"Table\": [\r\n      {\r\n      \"error\": \"Thêm sản phẩm thất bại }\r\n  ]\r\n}";
            }
            return(Json(jsonRs, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public async Task <JsonResult> changeUserAvatar(string userName, string userId, HttpPostedFileBase image)
        {
            string imageName = string.Format(Constant.avatarNameFormat, userId, Path.GetExtension(image.FileName).ToLower());
            string avatarUrl = await AzureBlobController.UploadImageAsync(image, imageName);

            if (avatarUrl == null)
            {
                jsonRs = "{\r\n  \"Table\": [\r\n      {\r\n      \"error\": \"Upload hình ảnh thất bại\"}\r\n  ]\r\n}";
            }
            else
            {
                ds     = ChoTot.Models.User.setUserAvatar(avatarUrl, userName, userId, image);
                jsonRs = JsonConvert.SerializeObject(ds, Formatting.Indented);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    Session["__USER__"] = jsonRs;

                    //Save cookies
                    try
                    {
                        FormsAuthentication.SetAuthCookie(userName, false);

                        if (Request.Cookies["ChoTotUser"] != null)
                        {
                            HttpCookie userCookie = new HttpCookie("ChoTotUser");
                            userCookie.Values["__USER__"] = jsonRs.ToString().Replace("\r\n", "");
                            userCookie.Expires.AddDays(1);
                            Response.Cookies.Add(userCookie);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("(Error - AuthCookie) Exception: ", ex);
                    }
                }
            }
            return(Json(jsonRs, JsonRequestBehavior.AllowGet));
        }