none
the given path's format is not supported. in MVC C# file upload RRS feed

  • Question

  • I've done the following code for uploading image:-

     [HttpPost]
            public ActionResult AddProduct(ProductVM model, HttpPostedFileBase file)
            {
                // Check model state
                if (!ModelState.IsValid)
                {
                    using (Db db = new Db())
                    {
                        model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                        return View(model);
                    }
                }
                // Make sure product name is unique
                using (Db db = new Db())
                {
                    if (db.Products.Any(x => x.Name == model.Name))
                    {
                        model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                        ModelState.AddModelError("", "That product name is taken!");
                        return View(model);
                    }
                }
                // Declare product id
                int id;
                // Init and save productDTO
                using (Db db = new Db())
                {
                    ProductDTO product = new ProductDTO();
                    product.Name = model.Name;
                    product.Slug = model.Name.Replace(" ", "-").ToLower();
                    product.Description = model.Description;
                    product.Price = model.Price;
                    product.CategoryId = model.CategoryId;
                    CategoryDTO catDTO = db.Categories.FirstOrDefault(x => x.Id == model.CategoryId);
                    product.CategoryName = catDTO.Name;
                    db.Products.Add(product);
                    db.SaveChanges();
                    // Get the id
                    id = product.Id;
                }
                // Set TempData message
                TempData["SM"] = "You have added a product!";
                #region Upload Image
                // Create necessary directories
                var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));
                var pathString1 = Path.Combine(originalDirectory.ToString(), "Products");
                var pathString2 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString());
                var pathString3 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Thumbs");
                var pathString4 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery");
                var pathString5 = Path.Combine(originalDirectory.ToString(), "Products\\" + id.ToString() + "\\Gallery\\Thumbs");
                if (!Directory.Exists(pathString1))
                    Directory.CreateDirectory(pathString1);
                if (!Directory.Exists(pathString2))
                    Directory.CreateDirectory(pathString2);
                if (!Directory.Exists(pathString3))
                    Directory.CreateDirectory(pathString3);
                if (!Directory.Exists(pathString4))
                    Directory.CreateDirectory(pathString4);
                if (!Directory.Exists(pathString5))
                    Directory.CreateDirectory(pathString5);
                // Check if a file was uploaded
                if (file != null && file.ContentLength > 0)
                {
                    // Get file extension
                    string ext = file.ContentType.ToLower();
                    // Verify extension
                    if (ext != "image/jpg" &&
                        ext != "image/jpeg" &&
                        ext != "image/pjpeg" &&
                        ext != "image/gif" &&
                        ext != "image/x-png" &&
                        ext != "image/png")
                    {
                        using (Db db = new Db())
                        {
                            model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name");
                            ModelState.AddModelError("", "The image was not uploaded - wrong image extension.");
                            return View(model);
                        }
                    }
                    // Init image name
                    string imageName = file.FileName;
                    // Save image name to DTO
                    using (Db db = new Db())
                    {
                        ProductDTO dto = db.Products.Find(id);
                        dto.ImageName = imageName;
                        db.SaveChanges();
                    }
                    // Set original and thumb image paths
                    var path = string.Format("{0}\\{1}", pathString2, imageName);
                    var path2 = string.Format("{0}\\{1}", pathString3, imageName);
                    // Save original
                    file.SaveAs(path);
                    // Create and save thumb
                    WebImage img = new WebImage(file.InputStream);
                    img.Resize(200, 200);
                    img.Save(path2);
                }
                #endregion

                // Redirect
                return RedirectToAction("AddProduct");
            }

    And after running this , it shows me the error that the given path's format is not supported.

    please help.

    thanks

    Sunday, March 17, 2019 1:38 PM

All replies