public ActionResult Edit([Bind(Include = "Id,Title,Imag,Introduction")] PopSciinManger popSciinManger) { string result = ""; string getimage = popSciinManger.Imag;//将读出来的图片url先传给getimage HttpPostedFileBase files = Request.Files["filename"]; //docManger.File = Request.Files["filename"]; string fileName = files.FileName; //Console.WriteLine(fileName); if (fileName == "") { popSciinManger.Imag = getimage;//如果getimage值是空的,就将原来的值还给Imag } else { string fileFormat = fileName.Split('.')[fileName.Split('.').Length - 1]; // 以“.”截取,获取“.”后面的文件后缀 Regex imageFormat = new Regex(@"^(bmp)|(png)|(gif)|(jpg)|(jpeg)"); // 验证文件后缀的表达式(这段可以限制上传文件类型) Console.WriteLine(Server.MapPath("~/")); if (string.IsNullOrEmpty(fileName) || !imageFormat.IsMatch(fileFormat)) // 验证后缀,判断文件是否是所要上传的格式 { result = "error"; } else { string timeStamp = DateTime.Now.Ticks.ToString(); // 获取当前时间的string类型 string firstFileName = timeStamp.Substring(0, timeStamp.Length - 4); // 通过截取获得文件名 string imageStr = "pic/"; // 获取保存附件的项目文件夹 string uploadPath = Server.MapPath("~/" + imageStr); // 将项目路径与文件夹合并 string pictureFormat = fileName.Split('.')[fileName.Split('.').Length - 1]; // 设置文件格式 string fileNames = firstFileName + "." + fileFormat; // 设置完整(文件名+文件格式) string saveFile = uploadPath + fileNames; //文件路径 files.SaveAs(saveFile); // 保存文件 // 如果单单是上传,不用保存路径的话,下面这行代码就不需要写了! result = "http://58.192.132.31:9011/" + imageStr + fileNames; // 设置数据库保存的路径 popSciinManger.Imag = result; //将新的图片地址传给Imag } } if (ModelState.IsValid) { MySqlConnection mysql = getMySqlConnection(); MySqlCommand mySqlCommand = getSqlCommand("UPDATE popsciinfortitle set title=" + "'" + popSciinManger.Title + "'" + ",introduction=" + "'" + popSciinManger.Introduction + "'" + ",Imag=" + "'" + popSciinManger.Imag + "'" + "where id=" + popSciinManger.Id, mysql); mysql.Open(); MySqlDataAdapter adapter = new MySqlDataAdapter(mySqlCommand); mySqlCommand.ExecuteNonQuery(); mysql.Close(); return(RedirectToAction("Index")); } return(View(popSciinManger)); }
public ActionResult DeleteConfirmed(int id) { PopSciinManger popSciinManger = new PopSciinManger(); MySqlConnection mysql = getMySqlConnection(); MySqlCommand mySqlCommand = getSqlCommand("DELETE FROM popsciinfortitle where id=" + id, mysql); mysql.Open(); MySqlDataAdapter adapter = new MySqlDataAdapter(mySqlCommand); mySqlCommand.ExecuteNonQuery(); mysql.Close(); return(RedirectToAction("Index")); }
// GET: PopSciinMangers/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } PopSciinManger popSciinManger = new PopSciinManger(); MySqlConnection mysql = getMySqlConnection(); MySqlCommand mySqlCommand = getSqlCommand("SELECT * FROM popsciinfortitle WHERE id=" + id, mysql); mysql.Open(); MySqlDataReader reader = mySqlCommand.ExecuteReader(); try { while (reader.Read()) { if (reader.HasRows) { popSciinManger.Id = reader.GetInt32("id"); popSciinManger.Title = reader.GetString("title"); popSciinManger.Imag = reader.GetString("imag"); string a = reader.GetString("introduction"); popSciinManger.Introduction = "<p>" + a.Replace("\r\n", "</p><p>") + "</p>"; // popSciinManger.Introduction = reader.GetString("introduction"); } } } catch { return(HttpNotFound()); } finally { mysql.Close(); } return(View(popSciinManger)); }
// GET: PopSciinMangers public ActionResult Index(int page = 1) { List <PopSciinManger> listpopSciinMangers = new List <PopSciinManger>(); MySqlConnection mysql = getMySqlConnection(); MySqlCommand mySqlCommand = getSqlCommand("select * from popsciinfortitle ", mysql); mysql.Open(); MySqlDataReader reader = mySqlCommand.ExecuteReader(); try { while (reader.Read()) { if (reader.HasRows) { PopSciinManger popSciinManger = new PopSciinManger(); popSciinManger.Id = reader.GetInt32("id"); string a; a = reader.GetString("title"); popSciinManger.Title = getstring(a, 30); popSciinManger.Imag = reader.GetString("imag"); listpopSciinMangers.Add(popSciinManger); } } } catch { return(HttpNotFound()); } finally { mysql.Close(); } const int pagesize = 8; var data = listpopSciinMangers .OrderBy(p => p.Id).ToPagedList(page, pagesize); return(View(data)); //return View(db.DocMangers.ToList()); }