C# 如何在MVC4中用图像保存模型
我编写了一个“产品创建”页面,将产品完美地保存到数据库中。。。产品具有ID、代码、名称属性。现在我还需要添加图像 我的旧代码如下C# 如何在MVC4中用图像保存模型,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我编写了一个“产品创建”页面,将产品完美地保存到数据库中。。。产品具有ID、代码、名称属性。现在我还需要添加图像 我的旧代码如下 [HttpPost] public ActionResult ProductsCreate(ProductDetailViewModel prod_) { if (ModelState.IsValid) { db.AY_PRODUCTS.Add(prod_.product); db.SaveChanges(); }
[HttpPost]
public ActionResult ProductsCreate(ProductDetailViewModel prod_)
{
if (ModelState.IsValid)
{
db.AY_PRODUCTS.Add(prod_.product);
db.SaveChanges();
}
}
如何更改视图和控制器以使用图像保存模型?将属性HttpPostedFileBase添加到模型中。在视图中使用它来发布图像
public HttpPostedFileBase YourFile { get; set; }
在您的操作中,将此图像保存到您的位置
在您的视图中:记住表单中的enctype=“multipart/form data”
<input name="YourFile" id="YourFile" type="file" accept="image/jpeg, image/png,image/jpg" value="">
提示:也许这不需要解决您的问题。您应该将操作命名为“创建”。如果我没有错的话,你有ProductController,对吗?因此,您只需将其命名即可创建,无需“ProductsCreate”。将属性HttpPostedFileBase添加到您的模型中。在视图中使用它来发布图像
public HttpPostedFileBase YourFile { get; set; }
在您的操作中,将此图像保存到您的位置
在您的视图中:记住表单中的enctype=“multipart/form data”
<input name="YourFile" id="YourFile" type="file" accept="image/jpeg, image/png,image/jpg" value="">
提示:也许这不需要解决您的问题。您应该将操作命名为“创建”。如果我没有错的话,你有ProductController,对吗?因此,您只需将其命名为创建,无需“ProductsCreate”。将其添加到模型中
[Display(Name = "Upload File")]
[DataType(DataType.Upload)]
public HttpPostedFileBase Image { get; set; }
在您的视图中,通过添加此enctype=“multipart/form data”
将此添加到模型中
[Display(Name = "Upload File")]
[DataType(DataType.Upload)]
public HttpPostedFileBase Image { get; set; }
在您的视图中,通过添加此enctype=“multipart/form data”
//鉴于
@using (Html.BeginForm("Create", "Banner", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
@Html.LabelFor(model => model.ImagePath)
</div>
<div class="editor-field">
<input id="File1" name="File1" type="file" />
@Html.ValidationMessageFor(model => model.ImagePath)
</div>
}
//clsUploadFile定义为
public static class clsUploadFile
{
public static string uploadFile(HttpPostedFileBase file, string UploadPath)
{
string imgPath = null;
//var fileName = DateTime.Now.ToString();
//fileName = fileName.Replace(" ", "_");
//fileName = fileName.Replace("/", "_");
//fileName = fileName.Replace(":", "_");
string fileName = System.IO.Path.GetRandomFileName();
fileName = fileName.Replace(".", "");
var ext = System.IO.Path.GetExtension(file.FileName);
fileName += ext;
System.IO.DirectoryInfo dr = new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~" + UploadPath));
System.IO.FileInfo[] files = dr.GetFiles();
for(;true;)
{
if (!files.Where(o => o.Name.Equals(fileName)).Any())
break;
else
{
fileName = System.IO.Path.GetRandomFileName();
fileName = fileName.Replace(".", "");
fileName += ext;
}
}
var path = System.IO.Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~" + UploadPath), fileName);
file.SaveAs(path);
imgPath = UploadPath + fileName;
return imgPath;
}
public static bool DeleteFile(string path)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(path));
if (file.Exists)
{
file.Delete();
return true;
}
else
return false;
}
}
//鉴于
@using (Html.BeginForm("Create", "Banner", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
@Html.LabelFor(model => model.ImagePath)
</div>
<div class="editor-field">
<input id="File1" name="File1" type="file" />
@Html.ValidationMessageFor(model => model.ImagePath)
</div>
}
//clsUploadFile定义为
public static class clsUploadFile
{
public static string uploadFile(HttpPostedFileBase file, string UploadPath)
{
string imgPath = null;
//var fileName = DateTime.Now.ToString();
//fileName = fileName.Replace(" ", "_");
//fileName = fileName.Replace("/", "_");
//fileName = fileName.Replace(":", "_");
string fileName = System.IO.Path.GetRandomFileName();
fileName = fileName.Replace(".", "");
var ext = System.IO.Path.GetExtension(file.FileName);
fileName += ext;
System.IO.DirectoryInfo dr = new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~" + UploadPath));
System.IO.FileInfo[] files = dr.GetFiles();
for(;true;)
{
if (!files.Where(o => o.Name.Equals(fileName)).Any())
break;
else
{
fileName = System.IO.Path.GetRandomFileName();
fileName = fileName.Replace(".", "");
fileName += ext;
}
}
var path = System.IO.Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~" + UploadPath), fileName);
file.SaveAs(path);
imgPath = UploadPath + fileName;
return imgPath;
}
public static bool DeleteFile(string path)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(path));
if (file.Exists)
{
file.Delete();
return true;
}
else
return false;
}
}
形象是产品的属性吗?你从哪里得到的图像?把你的表格也贴出来。看一下我的答案,让我知道它是否有效…图片是产品的属性吗?你从哪里得到的图像?把你的表格也贴出来。看看我的答案,让我知道它是否有效…视图部分如何?图像控件的名称应该是什么?视图部分如何?我应该给图像控件起什么名字?回答正确。非常合理的解决方案,也非常简单。正确答案。非常合理的解决方案,也非常简单。