1

I'm uploading an image to a folder images. it's working fine.but what I actually want is to look for a folder name (I have the folder name) if not found create that folder and give it that name.how could that happen?

this is what I have done so far:

 string ImageName = System.IO.Path.GetFileName(file.FileName); string physicalPath = Server.MapPath("~/images/" + ImageName); 

instead of images I should have folderName.

the complete view:

@{ ViewBag.Title = "Index"; } @using (Html.BeginForm("FileUpload", "datum", FormMethod.Post, new { enctype = "multipart/form-data" })) { <div> category<br /> @Html.DropDownList("category", ViewBag.Roles as SelectList) <br/> description<br /> @Html.TextBox("description") <br /> Image<br /> <input type="File" name="file" id="file" value="Choose File" /> <input type="submit" value="Upload" class="submit" /> </div> } 

the complete controller

 public class datumController : Controller { DataEntry db = new DataEntry(); public ActionResult Index() { var data = from p in db.categories select p.categoryName; SelectList list = new SelectList(data); ViewBag.Roles = list; return View(); } public ActionResult create () { return View(); } [HttpPost] public ActionResult FileUpload(HttpPostedFileBase file) { if (file != null) { string ImageName = System.IO.Path.GetFileName(file.FileName); string physicalPath = Server.MapPath("~/images/" + ImageName); // save image in folder file.SaveAs(physicalPath); //save new record in database datum newRecord = new datum(); newRecord.category = Request.Form["category"]; newRecord.description = Request.Form["description"]; newRecord.imagePath = ImageName; db.data.Add(newRecord); db.SaveChanges(); } //Display records return RedirectToAction("Display"); } 

so I should be getting the selected value from the drop down list and attach it to the physical path, check if folder exists if no then create folder and upload image to that folder

2 Answers 2

0

Try like below...

 string subPath ="ImagesPath"; // your code goes here bool exists = System.IO.Directory.Exists(Server.MapPath(subPath)); if(!exists) System.IO.Directory.CreateDirectory(Server.MapPath(subPath)); 

For further info, please refer below link. If a folder does not exist, create it

Sign up to request clarification or add additional context in comments.

5 Comments

the subpath is supposed to carry a selected value from a dropdownlist can you help me with that?
how you are getting subpath value from UI page?
this is what I am asking for :D to get the subpath value from the selected drop down list item.I will edit my code to include the view and the controller
ok I got the category from the drop downlist now it says at the save subpath that it should be a rooted pathalthough i'm using the tilda ~
Can you try to combine your category value(which is coming from dropdown) with your file name like below...Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); please refer following link..stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0 hope this will help you..
-1
if (file != null && file.ContentLength > 0) { string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(file.FileName)); tbl_MixEmp.EmpImage = Path.Combine("~/Images", file.FileName); file.SaveAs(path); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.