2014-10-28 1 views
0

나는 이미지를 폴더 이미지에 업로드하고있다. 괜찮아요.하지만 실제로 원하는 것은 폴더 이름을 찾지 못하면 폴더 이름을 찾고 그 폴더를 만들고 이름을 부여하는 것입니다. 어떻게 될까요?Server.MapPath 폴더를 확인하고 만들기

이 내가 지금까지 무엇을했는지 있습니다 :

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

을 대신 내가 폴더 이름을 가져야한다 이미지의.

전체 보기 :

@{ 
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> 
} 

전체 컨트롤러

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"); 
    } 

그래서 드롭 다운 목록에서 선택한 값을 받고 실제 경로에 첨부되어야한다, 폴더가 존재하는지 확인한 다음 폴더를 생성하고 해당 폴더에 이미지를 업로드하십시오.

답변

0

다음과 같이 시도하십시오 ...

자세한 내용은 아래 링크를 참조하십시오. If a folder does not exist, create it

+0

서브 패스는 당신이 날 그와 함께 도움을 줄 수있는 드롭 다운리스트에서 선택한 값을 수행 할 예정이다? – mohammad

+0

UI 페이지에서 서브 패스 값을 얻는 방법은 무엇입니까? – Vignesh

+0

이것이 내가 요구하는 것입니다 : D 선택한 하위 항목에서 하위 항목 값을 가져 오는 것입니다.보기 및 컨트롤러를 포함하도록 코드를 편집합니다. – mohammad

-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); 
}