Exemplo n.º 1
0
        public void Post(string barcode, string name, string description, int branchPrice, int stock, string branchOffice)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())

                    using (StreamReader jsonStream = new StreamReader(stream))
                    {
                        BranchInventory branchInventory = new BranchInventory
                        {
                            Barcode      = barcode,
                            Name         = name,
                            Description  = description,
                            BranchPrice  = branchPrice,
                            Stock        = stock,
                            BranchOffice = branchOffice
                        };


                        var            jsonOld = jsonStream.ReadToEnd();
                        DataBaseStruct list    = JsonConvert.DeserializeObject <DataBaseStruct>(jsonOld);
                        list.BranchInventories.Add(branchInventory);

                        //Serializar el json
                        var request2 = (HttpWebRequest)WebRequest.Create(url);
                        request2.Method      = "POST";
                        request2.ContentType = "application/json";
                        request2.Timeout     = 30000;

                        string jsonNew   = JsonConvert.SerializeObject(list);
                        byte[] byteArray = Encoding.UTF8.GetBytes(jsonNew);
                        request2.ContentLength = byteArray.Length;

                        using (var dataStream = request2.GetRequestStream())
                        {
                            dataStream.Write(byteArray, 0, byteArray.Length);
                        }

                        using (HttpWebResponse response3 = (HttpWebResponse)request2.GetResponse())
                        {
                            using (Stream stream2 = response3.GetResponseStream())
                            {
                                using (StreamReader reader = new StreamReader(stream2))
                                {
                                    string responseFromServer = reader.ReadToEnd();
                                }
                            }
                        }
                    }
        }
Exemplo n.º 2
0
        public JsonResult Import(int?id)
        {
            Response response;

            try
            {
                var                currentUser = GetAuthenticatedUser();
                string             serverPath  = Server.MapPath("~/Temp/");
                HttpPostedFileBase hpf         = Request.Files[0];
                if (hpf.ContentLength == 0)
                {
                    throw new Exception("File length can't be equal to zero");
                }

                string fileName      = Path.GetFileName(hpf.FileName);
                string savedFileName = Path.Combine(serverPath, fileName);

                if (System.IO.File.Exists(savedFileName))
                {
                    Random random = new Random();
                    string prefix = random.Next(1000, 9999).ToString() + "-";
                    fileName = prefix + fileName;
                }
                savedFileName = Path.Combine(serverPath, fileName);
                if (Path.GetExtension(savedFileName) != ".xlsx")
                {
                    response = new Response()
                    {
                        status  = 500,
                        message = "فرمت فایل اشتباه است."
                    };
                    return(Json(response, JsonRequestBehavior.AllowGet));
                }
                if (!Directory.Exists(serverPath))
                {
                    Directory.CreateDirectory(serverPath);
                }

                hpf.SaveAs(savedFileName);
                // Open the Excel file using ClosedXML.
                // Keep in mind the Excel file cannot be open when trying to read it
                IXLRange range;
                using (var db = new KiaGalleryContext())
                {
                    var branchId = id != null && id > 0 ? id : currentUser.BranchId;
                    var oldData  = db.BranchInventory.Where(x => x.BranchId == branchId);
                    db.BranchInventory.RemoveRange(oldData);
                    using (XLWorkbook wb = new XLWorkbook(savedFileName))
                    {
                        var ws = wb.Worksheets.First();
                        range = ws.RangeUsed();

                        var firstCell = ws.Cell(1, 1).Value.ToString();
                        if (string.IsNullOrEmpty(firstCell) || string.IsNullOrWhiteSpace(firstCell) || firstCell.Trim() != "نام")
                        {
                            response = new Response()
                            {
                                status  = 500,
                                message = "فرمت فایل اشتباه است."
                            };
                            return(Json(response, JsonRequestBehavior.AllowGet));
                        }
                        ws.FirstRow().Delete();
                        for (int i = 1; i < range.RowCount() + 1; i++)
                        {
                            var item = new BranchInventory()
                            {
                                Title         = ws.Cell(i, 1).Value.ToString(),
                                ProductCode   = ws.Cell(i, 2).Value.ToString(),
                                Weight        = ws.Cell(i, 3).Value.ToString(),
                                BranchId      = branchId.Value,
                                InventoryType = ws.Cell(i, 1).Value.ToString().Contains("*") ? InventoryType.Customer : InventoryType.Branch,
                                CreateUserId  = currentUser.Id,
                                ModifyUserId  = currentUser.Id,
                                CreateDate    = DateTime.Now,
                                ModifyDate    = DateTime.Now
                            };
                            db.BranchInventory.Add(item);
                        }
                    }
                    db.SaveChanges();
                }
                response = new Response()
                {
                    status  = 200,
                    message = "فایل با موفقیت بارگزاری شد." + " " + range.RowCount()
                };
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }