我知道这个问题很多次,但没有回答它对我有用。 我的观点就像......
<div class="form-group">
@Html.LabelFor(model => model.VHF, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.VHF)
@Html.ValidationMessageFor(model => model.VHF)
</div>
</div>
@using (Html.BeginForm("Create", "Radio", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="uploadFile" />
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
我的控制器......
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Model_No,Manufacturer_Id,Display,Channel_Capacity,Max_Freq_Separation,UHF_R1,UHF_R2,VHF,Pic")] sys_Radio_Models radioModels, HttpPostedFileBase uploadFile)
{
if (ModelState.IsValid)
{
if(uploadFile != null && uploadFile.ContentLength > 0)
{
//File size must be 2 MB as maximum
if (uploadFile.ContentLength > (2 * 1024 * 1024))
{
ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
return View();
}
//File types allowed : jpeg, jpg, png, gif and tif
if (!(uploadFile.ContentType == "image/jpeg"
|| uploadFile.ContentType == "image/jpg"
|| uploadFile.ContentType == "image/png"
|| uploadFile.ContentType == "image/gif"
|| uploadFile.ContentType == "image/tif"))
{
ModelState.AddModelError("CustomError", "File types allowed : jpeg, jpg, png, gif and tif");
return View();
}
byte[] data = new byte[uploadFile.ContentLength];
uploadFile.InputStream.Read(data, 0, uploadFile.ContentLength);
radioModels.Pic = data;
}
else
{
// Set the default image:
var img = Image.FromFile(Server.MapPath(Url.Content("~/assets/img/nopic.png")));
var ms = new MemoryStream();
img.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
radioModels.Pic = new byte[ms.Length];
ms.Read(radioModels.Pic, 0, (int)ms.Length);
}
db.sys_Radio_Models.Add(radioModels);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Manufacturer_Id = new SelectList(db.sys_Manufacturers, "Manufacturer_Id", "Manufacturer_Name_a", radioModels.Manufacturer_Id);
return View(radioModels);
}
那么,我的错误是什么......
由于