我正在建立一个简单的学校门户网站,我一直坚持将图像上传到我的应用程序中,即用户应将学校图像上传到我的服务器上,我的图像目录为./Content/Images-所有上传的图像都应上传到这个目录。我有以下代码
input type="file" id="SchoolImageUrl" name="SchoolImageUrl" class="required"
使用此获取浏览按钮,我不知道如何将图像上传到服务器,我的动作 Controller 将如何?我有以下创建学校的负责人
public ActionResult SchoolCreate(_ASI_School schoolToCreate, FormCollection collection) { if (!ModelState.IsValid) return View(); try { // TODO: Add insert logic here schoolToCreate.SchoolId = Guid.NewGuid().ToString(); schoolToCreate.UserId = new Guid(Request.Form["currentUser"]); schoolToCreate.SchoolAddedBy = User.Identity.Name; HttpPostedFileBase file = Request.Files["SchoolImageUrl"]; file.SaveAs(file.FileName); //schoolToCreate.SchoolImageUrl = Reuseable.ImageUpload(Request.Files["SchoolImageUrl"], Server.MapPath("../Content")); //schoolToCreate.SchoolImageUrl = Path.GetFullPath(Request.Files[0].FileName); schoolToCreate.SchoolImageUrl = collection["SchoolImageUrl"]; UpdateModel(schoolToCreate); _schoolRepository.CreateSchool(schoolToCreate); //_schoolRepository.SaveToDb(); return RedirectToAction("DepartmentCreate", "Department", new { userId = schoolToCreate.UserId, schoolId = schoolToCreate.SchoolId }); } catch { return View("CreationFailed"); } }
在这里我得到对象引用错误

最佳答案

看看this post

HttpPostedFileBase file = Request.Files["SchoolImageUrl"];

可能是造成它的原因。您是否调试过以检查其是否为空值?

09-25 19:50