Image 如何在ASP.NET MVC3中将映像上载到sql server
在我的ASP.NET MVC3 RAZOR项目中,我必须实现图像上传。我尝试了不同的教程,但没有一个适合我。我有一个模型、控制器和一个视图文件。 作为搜索操作,从搜索操作中,客户列表将加载上载文件的选项。 这里我的问题是如何使用ASP.NETMVC3将图像上载到SQLServer 模型代码Image 如何在ASP.NET MVC3中将映像上载到sql server,image,asp.net-mvc-3,razor,file-upload,Image,Asp.net Mvc 3,Razor,File Upload,在我的ASP.NET MVC3 RAZOR项目中,我必须实现图像上传。我尝试了不同的教程,但没有一个适合我。我有一个模型、控制器和一个视图文件。 作为搜索操作,从搜索操作中,客户列表将加载上载文件的选项。 这里我的问题是如何使用ASP.NETMVC3将图像上载到SQLServer 模型代码 namespace Elixir.Models { [Table("tbl_ElixiCustPro")] public class ElixiCustPro { [K
namespace Elixir.Models
{
[Table("tbl_ElixiCustPro")]
public class ElixiCustPro
{
[Key]
public int ImageId { get; set; }
public int CusId { get; set; }
public string CustomerName { get; set; }
public string ImageUrl { get; set; }
public byte[] Image { get; set; }
}
}
控制器
[HttpGet]
public ActionResult UploadPhoto()
{
return View();
}
[HttpPost]
public ActionResult UploadPhoto(ElixiCustPro elixi, HttpPostedFileBase uploadfile, int CusId,string Name)
{
if (uploadfile != null && uploadfile.ContentLength > 0)
{
elixi.Image = new byte[uploadfile.ContentLength];
uploadfile.InputStream.Read(elixi.Image, 0, uploadfile.ContentLength);
}
try
{
if (ModelState.IsValid)
{
ecp.Image = new byte[uploadfile.ContentLength];
ecp.ImageUrl = "";
ecp.CustomerName = Name;
ecp.CusId = CusId;
ment.ElixiProData.Add(ecp);
ment.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
return View();
}
return View();
}
视图代码
<legend>File Management</legend>
@using (Html.BeginForm("UploadPhoto", "Home",FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
<label class="col-lg-2 control-label">
Customer ID</label>
<div class="col-lg-10">@Html.TextBoxFor(model => model.CusId, new { @class = "form-control" })</div>
<label class="col-lg-2 control-label">
Customer Name</label>
<div class="col-lg-10">@Html.TextBoxFor(model => model.Name, new { @class = "form-control" })</div>
</div>
<input type="hidden" id="id" />
<div class="col-md-6">
<div class="form-group">
<label class="col-lg-2 control-label">
DMIT Image</label>
<div class="col-lg-10">
<input type="file" id="file" name="file">
<input type="submit" class="btn btn-success" value="Upload" />
</div>
</div>
</div>
}
文件管理
@使用(Html.BeginForm(“UploadPhoto”,“Home”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
客户ID
@TextBoxFor(model=>model.CusId,新的{@class=“form control”})
客户名称
@Html.TextBoxFor(model=>model.Name,新的{@class=“form control”})
DMIT图像
}
到目前为止,您尝试了什么?byte array属性应与计算机上的任何其他属性一样可保存object@AlexandrMihalciuc如何保存它。。我尝试了不同的代码,但没有一个对我有效。所以我删除了那些代码。你试图在数据库中保存模型的代码是什么?你犯了什么错误?不要从你的问题中省略任何代码。否则你怎么能指望我们告诉你你的代码出了什么问题?你甚至没有显示它。@DarinDimitrov我已经用删除的代码更新了问题。。当我单击“上载”按钮时,它只是刷新页面而没有进入页面controller@AlexandrMihalciuc我已经用删除的代码更新了问题。