ASP.NET MVC+Easyui 后台管理系统的图片上传
实现图片的上传
easyui代码部分:
//添加按钮 var URL; $("#btnCreate").click(function () { $('#UserDialog').dialog('open').dialog('setTitle', '添加产品'); $('#ff').form('clear'); //用户清除form表单中的记录 但是写在这里好像没用 URL = "/HouTai_ShangP/CreateUser"; })
后台控制器代码部分:
public ActionResult CreateUser(Product user) { //设置用户信息的创建时间 string file = Request["file"]; HttpFileCollectionBase files = Request.Files; HttpPostedFileBase fileesa = files["file"]; string extName = Path.GetExtension(fileesa.FileName).ToLower(); if (extName != ".jpg") { string ls = "上传了不被识别的格式,请重新上传"; return Content(ls); } string path = Server.MapPath("~/UploadFile/"); //生成新文件的名称,guid保证某一时刻内唯一的(保证了文件不会被覆盖) string fileNewName = System.Guid.NewGuid().ToString(); fileesa.SaveAs(path + fileNewName + extName); string file2 = Request["file2"]; HttpFileCollectionBase files2 = Request.Files; HttpPostedFileBase fileesa2 = files2["file2"]; string extName2 = Path.GetExtension(fileesa2.FileName).ToLower(); if (extName2 != ".jpg") { string ls2 = "上传了不被识别的格式,请重新上传"; return Content(ls2); } string path2 = Server.MapPath("~/weitu/"); //生成新文件的名称,guid保证某一时刻内唯一的(保证了文件不会被覆盖) string fileNewName2 = System.Guid.NewGuid().ToString(); fileesa2.SaveAs(path2 + fileNewName2 + extName2); user.AddTime = DateTime.Now; //因为我这边需要上传7张图片所以有点多 user.ProductPic = "UploadFile/" + fileNewName + extName; user.xiaotu1 = "UploadFile/" + fileNewName + extName; user.xiaotu2 = "UploadFile/" + fileNewName + extName; user.xiaotu3 = "UploadFile/" + fileNewName + extName; user.xiaotu4 = "UploadFile/" + fileNewName + extName; user.xiaotu5 = "UploadFile/" + fileNewName + extName; user.weitu = "weitu/" + fileNewName2 + extName2; db.Product.Add(user); db.SaveChanges(); var obj = new { success = "true", message = "OK" }; return Json(obj, "text/plain", JsonRequestBehavior.AllowGet); }