Model view controller 检查文件夹的Server.MapPath并创建
我正在将图像上载到文件夹“图像”。它工作正常。但我真正想要的是查找一个文件夹名。如果找不到,我有文件夹名。创建该文件夹并为其命名。怎么会发生这种情况 这就是我到目前为止所做的:Model view controller 检查文件夹的Server.MapPath并创建,model-view-controller,Model View Controller,我正在将图像上载到文件夹“图像”。它工作正常。但我真正想要的是查找一个文件夹名。如果找不到,我有文件夹名。创建该文件夹并为其命名。怎么会发生这种情况 这就是我到目前为止所做的: string ImageName = System.IO.Path.GetFileName(file.FileName); string physicalPath = Server.MapPath("~/images/" + ImageName); 我应该使用folderName而不是图像 完整视图: 因此,我应该
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/images/" + ImageName);
我应该使用folderName而不是图像
完整视图:
因此,我应该从下拉列表中获取所选值并将其附加到物理路径,检查文件夹是否存在,如果不存在,然后创建文件夹并将图像上载到该文件夹中,尝试如下操作
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));
有关更多信息,请参阅下面的链接。
子路径应该包含从下拉列表中选择的值,你能帮我吗?你是如何从UI页面获取子路径值的?这就是我的要求:D从所选下拉列表项获取子路径值。我将编辑代码,以包含视图和控制器。我现在从下拉列表中获取类别它在save子路径上说它应该是根路径尽管我使用的是tilda~你能尝试将下拉列表中的类别值与下面的文件名组合起来吗…Path.CombineServer.MapPath~/App_Data/uploads,fileName;请参考以下链接。。希望这对你有帮助。。
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");
}
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));
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);
}