public ActionResult 新增產品(ProductViewModel n新增產品)
        {
            MotaiDataEntities db   = new MotaiDataEntities();
            tProduct          prod = new tProduct();

            prod.pNumber   = n新增產品.pNumber;
            prod.pName     = n新增產品.pName;
            prod.pCategory = n新增產品.pCategory;
            prod.pMaterial = n新增產品.pMaterial;
            prod.pSize     = n新增產品.pSize;
            prod.pLxWxH    = n新增產品.pLxWxH;
            prod.pPrice    = n新增產品.pPrice;
            prod.pWeight   = n新增產品.pWeight;
            db.tProducts.Add(prod);

            tProduct Product = db.tProducts.OrderByDescending(o => o.ProductId).FirstOrDefault();
            int      ProductId;

            if (Product == null)
            {
                ProductId = 1;
            }
            else
            {
                ProductId = Product.ProductId;
                ProductId++;
            }
            if (n新增產品.pImage.Count() > 0)
            {
                foreach (var uploagFile in n新增產品.pImage)
                {
                    if (uploagFile.ContentLength > 0)
                    {
                        tProductImage image     = new tProductImage();
                        FileInfo      file      = new FileInfo(uploagFile.FileName);
                        string        photoName = Guid.NewGuid().ToString() + file.Extension;
                        uploagFile.SaveAs(Server.MapPath("~/images/" + photoName));
                        image.ProductId = ProductId;
                        image.pImage    = "~" + Url.Content("~/images/" + photoName);
                        db.tProductImages.Add(image);
                    }
                }
            }
            db.SaveChanges();
            return(RedirectToAction("倉儲看產品頁面"));
        }
        public ActionResult 修改產品(EmpProductViewModel p)
        {
            if (Session[CSession關鍵字.SK_LOGINED_EMPLOYEE] == null)
            {
                return(RedirectToAction("員工登入", "Employee"));
            }
            MotaiDataEntities dbContext = new MotaiDataEntities();
            tProduct          prod      = dbContext.tProducts.Find(p.ProductId);

            if (prod != null)
            {
                prod.pNumber   = p.pNumber;
                prod.pName     = p.pName;
                prod.pCategory = p.pCategory;
                prod.pMaterial = p.pMaterial;
                prod.pSize     = p.pSize;
                prod.pLxWxH    = p.pLxWxH;
                prod.pWeight   = p.pWeight;
                prod.pPrice    = p.pPrice;
                List <tProductImage> oldImages = dbContext.tProductImages.Where(imgs => imgs.ProductId.Equals(p.ProductId)).ToList();
                if (oldImages.Count > p.pImage.Count)
                {
                    int index = 0;
                    foreach (var oldItem in oldImages)
                    {
                        if (index < p.pImage.Count)
                        {
                            if (p.pImage[index] == null)
                            {
                                break;
                            }
                            if (p.pImage[index].ContentLength > 0)
                            {
                                FileInfo file      = new FileInfo(p.pImage[index].FileName);
                                string   photoName = Guid.NewGuid().ToString() + file.Extension;
                                p.pImage[index].SaveAs(Server.MapPath("~/images/" + photoName));
                                oldItem.pImage = Url.Content("~/images/" + photoName);
                                //Directory.Delete(Url.Content(oldItem.pImage));
                            }
                        }
                        else
                        {
                            dbContext.tProductImages.Remove(oldItem);
                        }
                        index++;
                    }
                }
                else
                {
                    int index = 0;
                    foreach (var item in p.pImage)
                    {
                        if (index < oldImages.Count)
                        {
                            FileInfo file      = new FileInfo(item.FileName);
                            string   photoName = Guid.NewGuid().ToString() + file.Extension;
                            item.SaveAs(Server.MapPath("~/images/" + photoName));
                            oldImages[index].pImage = Url.Content("~/images/" + photoName);
                        }
                        else
                        {
                            tProductImage image     = new tProductImage();
                            FileInfo      file      = new FileInfo(item.FileName);
                            string        photoName = Guid.NewGuid().ToString() + file.Extension;
                            item.SaveAs(Server.MapPath("~/images/" + photoName));
                            image.ProductId = p.ProductId;
                            image.pImage    = "~" + Url.Content("~/images/" + photoName);
                            dbContext.tProductImages.Add(image);
                        }
                        index++;
                    }
                }
                dbContext.SaveChanges();
            }
            return(RedirectToAction("倉儲看產品頁面"));
        }
Esempio n. 3
0
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string productTypeCode = hdProductTypeCode.Value;
        string branchType = hdBranchType.Value;
        string imageThumb = "", imageZoom = "";
        string filename = "", img_path = "", img_save = "";
        string _control = "";
        var    p = from x in db.tProductImages
                   where x.BranchTypeId == int.Parse(branchType.Trim()) &&
                   x.ProductTypeCode == productTypeCode.Trim()
                   select x;

        if (p.Count() > 0)
        {
            //update image
            Response.Redirect("/image");
        }
        else
        {
            //inser image
            if (fuImage.HasFile || fuThumb.HasFile)
            {
                string             date = DateTime.Now.ToString("ddMMyyyyHHmmss");
                HttpFileCollection hfc  = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    _control = hfc.AllKeys[i];
                    if (hpf.ContentLength > 0)
                    {
                        filename = fuImage.FileName.Trim();

                        if (_control.Contains("fuImage"))
                        {
                            img_path = "/upload/product/" + date + "_" + i.ToString() + filename.Substring(filename.LastIndexOf('.'));
                            if (imageZoom != "")
                            {
                                imageZoom += "#";
                            }
                            imageZoom += img_path;
                        }
                        else
                        {
                            img_path = "/upload/product/" + "thumb_" + date + "_" + i.ToString() + filename.Substring(filename.LastIndexOf('.'));
                            if (imageThumb != "")
                            {
                                imageThumb += "#";
                            }
                            imageThumb += img_path;
                        }

                        if (branchType == "1")
                        {
                            img_save = "/upload/cnice/product/" + date + "_" + i.ToString() + filename.Substring(filename.LastIndexOf('.'));
                        }
                        else if (branchType == "2")
                        {
                            img_save = "/upload/cfashion/product/" + date + "_" + i.ToString() + filename.Substring(filename.LastIndexOf('.'));
                        }
                        else
                        {
                            img_save = "/upload/cn/product/" + date + "_" + i.ToString() + filename.Substring(filename.LastIndexOf('.'));
                        }

                        hpf.SaveAs(Server.MapPath(img_save));
                    }
                }
            }
            //insert data
            var im = new tProductImage();
            im.BranchTypeId    = int.Parse(branchType.Trim());
            im.ProductTypeCode = productTypeCode.Trim();
            im.ImageThumb      = imageThumb.Trim();
            im.ImageZoom       = imageZoom.Trim();
            im.Status          = 1;
            db.tProductImages.InsertOnSubmit(im);
            db.SubmitChanges();
        }
        Response.Redirect("/image");
    }