public void AddChemicalDevicePicPost() { chemical_device_pic ep = new chemical_device_pic(); HttpPostedFileBase ff = Request.Files["File"]; /*if (ff != null && ff.ContentLength != 0) * { * if (!Directory.Exists(Server.MapPath("~images/ElectricalPic"))) * { * Directory.CreateDirectory("~images/ElectricalPic"); * } * }*/ //修改文件名去掉其原有的扩展名 string filename = Request.Form["filename2"]; string[] filename_temp = filename.Split('.'); filename = filename_temp[0]; //读取elec_pic表最后一个id号加1最为不重复图片名的依据 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SewagePlantIMS"].ConnectionString); con.Open(); string sql = "select max(id) from dm_chemical_device_pic"; SqlCommand cmd1 = new SqlCommand(sql, con); int max_id; if (cmd1.ExecuteScalar() != DBNull.Value) { max_id = Convert.ToInt32(cmd1.ExecuteScalar()); max_id++; } else { max_id = 1; } string filepath = Server.MapPath("/images/ChemicalDevicePic_Pre/" + filename + "_" + Request.Form["id"] + "_" + max_id.ToString() + ".png"); ff.SaveAs(filepath); //在服务器上保存上传原图文件 //string[] readFile = System.IO.File.ReadAllLines(filepath);//读取txt文档存放在字符数组中 string filepath2 = Server.MapPath("/images/ChemicalDevicePic/" + filename + "_" + Request.Form["id"] + "_" + max_id.ToString() + ".png"); CompressPic cp = new CompressPic(); bool temp = cp.CompressImage(filepath, filepath2, 80, 150, true); //保存压缩完的文件到ChemicalDevicePic文件夹 string sqlStr = "insert into dm_chemical_device_pic (cd_picname,cd_picurl,cd_addtime,cd_id) values('" + filename + "','" + "/images/ChemicalDevicePic/" + filename + "_" + Request.Form["id"] + "_" + max_id.ToString() + ".png" + "','" + DateTime.Now.ToString() + "','" + Request.Form["id"] + "')"; SqlCommand cmd = new SqlCommand(sqlStr, con); int check = cmd.ExecuteNonQuery(); if (check == 1) { Response.Write("<script>alert('添加图片成功!!');window.location.href='AddChemicalDevicePic?id=" + Request.Form["id"] + " ' ;</script>"); } else { Response.Write("<script>alert('添加图片失败!,请手动联系管理员~');</script>"); } con.Close(); //return RedirectToAction("Electrical_List"); //如果直接View就不会执行HttpGet的代码 //return ElectricalPic(); }
public ActionResult AddChemicalDevicePic() { ViewBag.cd_id = Request.Form["id"]; List <chemical_device_pic> models = new List <chemical_device_pic>(); SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SewagePlantIMS"].ConnectionString); string sqlStr = "select id,cd_picurl from dm_chemical_device_pic where cd_id = " + ViewBag.cd_id + "; "; SqlDataAdapter da = new SqlDataAdapter(sqlStr, con); DataSet ds = new DataSet(); da.Fill(ds); chemical_device_pic[] tc = new chemical_device_pic[ds.Tables[0].Rows.Count]; for (int mDr = 0; mDr < ds.Tables[0].Rows.Count; mDr++) { tc[mDr] = new chemical_device_pic(); tc[mDr].id = Convert.ToInt32(ds.Tables[0].Rows[mDr][0]); tc[mDr].cd_picurl = ds.Tables[0].Rows[mDr][1].ToString(); models.Add(tc[mDr]); } con.Close(); return(View(models)); }