model view controller - Server.MapPath check for folder and create -


I am uploading an image to one of the image images, it's working fine. But what I really want, Look at that folder name (the name of the folder I have), if that folder is not found and name it. This can happen?

This is what I have done so far:

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

Instead of images, I should have a folder name.

Full View :

  @ {ViewBag. Title = "index"; } Using @using (Html.BeginForm ("FileUpload", "datum", FormMethod.Post, new {enctype = "multipart / form-data"})) {& lt; Div & gt; Category & lt; Br / & gt; @ Html.DropDownList ("Category", see Bag.Roles as a selector) & lt; Br / & gt; Details & lt; Br / & gt; @html Textbox ("Details") & lt; Br / & gt; Image & lt; Br / & gt; & Lt; Input type = "file" name = "file" id = "file" value = "file" /> & Lt; Input type = "submit" value = "upload" class = "submit" /> & Lt; / Div & gt; }  

Full Controller

  Public Class Data Controller: Controller {DataEntry db = New Data Entry (); Select public performance index () {db.categories in {var data = p.categoryName; Selection list list = new selection list (data); ViewBag.Roles = list; See Return (); } Create Public Action () {Return View (); } [HTTPOst] Public Action Result File Upload (HTTP Posted FileBase File) {if (file! = Null) {string ImageName = System.IO.Path.GetFileName (file.FileName); String physicalPath = Server.MapPath ("~ / images /" + ImageName); // Save the image in the folder file. Save A (physical path); // Save new record in database; newrecord = new datum (); NewRecord.category = Request.form ["Category"]; NewRecord.description = Requests Farm ["description"]; Newreecord.imagePath = ImageName; Db.data.Add (newRecord); Db.SaveChanges (); } // Display Records Redirect Redirection ("Display"); }  

So I should get the selected value from the drop down list and attach it to the physical path, check that the folder exists if the folder is not created and upload the image to that folder < / P>

Try the following ...

  string subpath = " Imagespath "; // Your code exists here bool = system.IO.Directory.Exists (Server.MapPath (subPath)); If (! Exists) System.IO.Directory.CreateDirectory (Server.MapPath (subPath));  

For more information, please see the link below.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -