我正在將圖像上傳到文件夾圖像。它工作正常。但我真正想要的是尋找一個文件夾名稱(我有文件夾名稱),如果沒有找到創建該文件夾,並給它的name.how可以發生?Server.MapPath檢查文件夾並創建
這是我迄今所做的:
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/images/" + ImageName);
,而不是我應該有FOLDERNAME圖像。
完全視圖:
@{
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");
}
所以我應該從下拉列表中得到選擇的值,並將其連接到物理路徑,檢查文件夾是否存在,如果沒有則創建文件夾並將圖像上傳到該文件夾
子路徑應該從下拉列表進行選擇的值,你可以幫我嗎? – mohammad 2014-10-28 11:56:49
如何從UI頁面獲取子路徑值? – Vignesh 2014-10-28 12:03:34
這就是我所要求的:D從選定的下拉列表項目中獲取子路徑值。我將編輯我的代碼以包括視圖和控制器 – mohammad 2014-10-28 12:06:30