C# HttpPostedFileBase到varbinary(最大值)
我刚刚尝试了很多我发现的东西,但最终没有成功 首先,我有下一个代码,只是做的事情好,但不保存图像 如何将图像保存到varbinarymax中?如何向下一个视图显示它们 视图:C# HttpPostedFileBase到varbinary(最大值),c#,asp.net-mvc-4,varbinarymax,C#,Asp.net Mvc 4,Varbinarymax,我刚刚尝试了很多我发现的东西,但最终没有成功 首先,我有下一个代码,只是做的事情好,但不保存图像 如何将图像保存到varbinarymax中?如何向下一个视图显示它们 视图: @LabelFor(model=>model.Logo,htmlAttributes:new{@class=“controllabel col-md-2”}) @*@EditorFor(model=>model.Logo,new{htmlAttributes=new{@class=“form control”})*@ @
@LabelFor(model=>model.Logo,htmlAttributes:new{@class=“controllabel col-md-2”})
@*@EditorFor(model=>model.Logo,new{htmlAttributes=new{@class=“form control”})*@
@Html.TextBoxFor(model=>model.Logo,新的{type=“file”})
@Html.ValidationMessageFor(model=>model.Logo,“,new{@class=“text danger”})
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school)
{
if (ModelState.IsValid)
{
db.School.Add(school);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(school);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建([Bind(Include=“Id,Name,Address,Description,Mail,Phone,Small_Description”)]School)
{
if(ModelState.IsValid)
{
db.School.Add(学校);
等待db.saveChangesSync();
返回操作(“索引”);
}
返回视图(学校);
}
型号:
public partial class School
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public School()
{
this.Product = new HashSet<Product>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Description { get; set; }
public string Mail { get; set; }
public int? Phone { get; set; }
public byte[] Logo { get; set; }
public string Small_Description { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product> Product { get; set; }
}
公立部分班学校
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公立学校()
{
this.Product=new HashSet();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字符串说明{get;set;}
公共字符串邮件{get;set;}
公共int?电话{get;set;}
公共字节[]徽标{get;set;}
公共字符串小描述{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection产品{get;set;}
}
由于您还没有发布完整的表单,下面是上传图像并保存到数据库的完整代码
表单必须具有enctype属性
@using (Html.BeginForm("Index","Home",FormMethod.Post, new{ enctype = "multipart/form-data" }))
{
//your other code
<input type="file" name="logo" />
<input type="submit" value="Save" />
}
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
//你的其他代码
}
在你的行动中
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school)
{
if (ModelState.IsValid)
{
byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files["logo"].InputStream))
{
fileData = binaryReader.ReadBytes(Request.Files["logo"].ContentLength);
}
school.Logo=fileData;
db.School.Add(school);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(school);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建([Bind(Include=“Id,Name,Address,Description,Mail,Phone,Small_Description”)]School)
{
if(ModelState.IsValid)
{
字节[]fileData=null;
使用(var binaryReader=new binaryReader(Request.Files[“logo”].InputStream))
{
fileData=binaryReader.ReadBytes(Request.Files[“logo”].ContentLength);
}
Logo=fileData;
db.School.Add(学校);
等待db.saveChangesSync();
返回操作(“索引”);
}
返回视图(学校);
}
它会将文件保存在数据库中 首先将视图更改为:
@using (Html.BeginForm("Create", "Schole", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(model => model.Logo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Logo, new { type = "file" })
<input type="submit" value="submit" />
</div>
</div>
}
@使用(Html.BeginForm(“Create”、“Schole”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@LabelFor(model=>model.Logo,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.TextBoxFor(model=>model.Logo,新的{type=“file”})
}
将操作更改为:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school, HttpPostedFileBase Logo)
{
if (ModelState.IsValid)
{
using (var memoryStream = new MemoryStream())
{
Logo.InputStream.CopyTo(memoryStream);
school.Logo = memoryStream.ToArray();
}
db.School.Add(school);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(school);
}
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务创建([Bind(Include=“Id,Name,Address,Description,Mail,Phone,Small_Description”)]学校,HttpPostedFileBase徽标)
{
if(ModelState.IsValid)
{
使用(var memoryStream=new memoryStream())
{
Logo.InputStream.CopyTo(memoryStream);
school.Logo=memoryStream.ToArray();
}
db.School.Add(学校);
等待db.saveChangesSync();
返回操作(“索引”);
}
返回视图(学校);
}
}
现在这个标志保存它
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,Name,Address,Description,Mail,Phone,Small_Description")] School school, HttpPostedFileBase Logo)
{
if (ModelState.IsValid)
{
using (var memoryStream = new MemoryStream())
{
Logo.InputStream.CopyTo(memoryStream);
school.Logo = memoryStream.ToArray();
}
db.School.Add(school);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(school);
}
}