I have a controller method
that works fine to upload images in Uploads
folder. I try to create a date folder in it, so that I can group the images in relevant date folder. But Directory.CreateDirectory()
is not creating a folder inside my existing Uploads
Folder.
I get an exception on SaveAs()
method that
can't find the directory: MyApp/Uploads/10-11-2015
My Action Method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ProductViewModel productViewModel)
{
var ValidImageTypes = new string[]{
"image/gif",
"image/jpeg",
"image/jpg",
"image/png"
};
if(!ValidImageTypes.Contains(productViewModel.ImageUpload.ContentType))
{
ModelState.AddModelError("ImageUpload", "Please upload gif / jpg / png");
}
if (ModelState.IsValid)
{
var product = new Products
{
Name = productViewModel.Name
};
var UploadDir = "/Uploads/" + DateTime.Now.ToString("dd/MM/yyyy") + "";
var FolderUploadDir = DateTime.Now.ToString("dd/MM/yyyy");
Directory.CreateDirectory("~/Uploads/" + FolderUploadDir);
//Saving the image
productViewModel.ImageUpload.SaveAs(ImagePath);
return View("Index");
}