//一般处理程序 public void GetImageFromWeb() { //创建文件夹 //2016-10-14 dq string filePath = "~/ProductImage/"; string mapFilePath; mapFilePath = HttpContext.Current.Server.MapPath(filePath); if (!Directory.Exists(mapFilePath)) { Directory.CreateDirectory(mapFilePath); } filePath += userEntity.User_ID + "/"; mapFilePath = HttpContext.Current.Server.MapPath(filePath); if (!Directory.Exists(mapFilePath)) { Directory.CreateDirectory(mapFilePath); } //2016-10-14 注释 dq //写入数据库的路径 string imgUrl = "/ProductImage/" + userEntity.User_ID + "/"; // 图片数据 HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; string fileName = files[0].FileName; string name = System.IO.Path.GetFileName(fileName); Stream stream = files[0].InputStream; byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); ResImgUpLoadData entity = WS.UploadImgs(bytes, mapFilePath, name, imgUrl, userEntity); BaseReturn(entity); }
//C#代码 #region 图片上传 /// <summary> // 功能简述:接口公开方法,图片上传 // 开发文档:无 // 开发团队:新产品组 // 设计:邓 // 分析:邓 // 代码:邓 // 开发时间:2016.3.10 // 代码检查: // 是否在使用:是 /// <summary> /// 上传图片 /// </summary> /// <param name="bytes">图片信息</param> /// <param name="pFileName">文件夹名称</param> /// <param name="pImgName">图片名称</param> /// <param name="pImgPath">写入数据库的图片路径</param> /// <param name="pUserInfo">操作人</param> /// <returns></returns> /// public ProductOnShelvesDML.ResImgUpLoadData UploadImgs(byte[] bytes, string pFileName, string pImgName, string pImgPath, UserDML.UserLoginInfo pUserInfo) { return pUploadImgs(bytes, pFileName,pImgName, pImgPath,pUserInfo); } private ProductOnShelvesDML.ResImgUpLoadData pUploadImgs(byte[] bytes,string pFileName,string pImgName, string pImgPath,UserDML.UserLoginInfo pUserInfo) { ProductOnShelvesDML.ResImgUpLoadData objResult=new ProductOnShelvesDML.ResImgUpLoadData(); //V_User_DB_Info objUserDBRes; //PublicCommonDML.ResComm objResComm; //Common objCommonDB; //objResult = new ProductOnShelvesDML.ResImgUpLoadData(); //objCommonDB = new Common(); Common objCommonDB = new Common(); PublicCommonDML.ResComm objResComm = new PublicCommonDML.ResComm(); ; try { string CommomString = "";//保存连接实体String // 验证用户信息,并获得连接实体 // 代码:邓奇 // 开发时间:2016-10-17 SYSCEntityDBAddLogic objSYSCEntityDBAddLogic = new SYSCEntityDBAddLogic(); objResComm = objSYSCEntityDBAddLogic.GetUserDBString(pUserInfo.User_ID, pUserInfo.User_PWD); if (objResComm.ResState == PublicCommonDML.ResFlag.Success) { CommomString = objSYSCEntityDBAddLogic.ConectionSring; } else { objResult.ResComm = objResComm; return objResult; } //服务器端创建文件夹并保存在WS端 //string filePath = "myImg"; //string mapFilePath; //mapFilePath = HttpContext.Current.Server.MapPath(filePath); //if (!Directory.Exists(mapFilePath)) // Directory.CreateDirectory(mapFilePath); //filePath += pUserInfo.User_ID + "/"; //mapFilePath = HttpContext.Current.Server.MapPath(filePath); //if (!Directory.Exists(mapFilePath)) // Directory.CreateDirectory(mapFilePath); //filePath += pUserInfo.User_ID + "/"; //mapFilePath = HttpContext.Current.Server.MapPath(filePath); //if (!Directory.Exists(mapFilePath)) // Directory.CreateDirectory(mapFilePath); // 图片数据 //Stream stream = new MemoryStream(bytes);//数据转换 //MemoryStream ms = new MemoryStream(); //System.Drawing.Image img = System.Drawing.Image.FromStream(stream); //img.Save(pFileName + pImgName);//保存 //ms.Close(); //string path = mapFilePath + pImgName; //objResult.ResComm.ResState = PublicCommonDML.ResFlag.Success; //objResult.ResComm.ResMessage = path;//返回图片路径 //return objResult; ////pFileName-----不需从前端传进,由配置文件设置 //string PhysicalAddress = System.Configuration.ConfigurationSettings.AppSettings["PhysicalAddress"];//存放图片的物理位置以及名称 //pFileName = @PhysicalAddress; //pFileName += pUserInfo.User_ID + "\\"; //if (!Directory.Exists(pFileName)) //{ // Directory.CreateDirectory(pFileName);//创建文件夹 //} //string IPAddress = System.Configuration.ConfigurationSettings.AppSettings["IPAddress"];//存放图片的物理位置对应的项目IP //创建文件夹(在一般处理程序完成)并保存在WEB端 //限制图片格式 bool Isimage = false; string suff = pImgName.Substring(pImgName.LastIndexOf('.'), pImgName.Length - pImgName.LastIndexOf('.')); //获取文件后缀 string[] allowExtension = { ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".JPG", ".JPEG", ".GIF", ".BMP", ".PNG" }; for (int i = 0; i < allowExtension.Length; i++) { if (suff == allowExtension[i]) { Isimage = true; break; } else { Isimage = false; } } if (!Isimage) { objResult.ResComm.ResState = PublicCommonDML.ResFlag.Fail; objResult.ResComm.ResMessage = "图片格式不正确,上传失败"; return objResult; } // 图片数据 Stream stream = new MemoryStream(bytes);//数据转换 if (stream.Length > 1024000) //1M { objResult.ResComm.ResState = PublicCommonDML.ResFlag.Fail; objResult.ResComm.ResMessage = "图片大小超过1M,上传失败"; return objResult; } else { MemoryStream ms = new MemoryStream(); System.Drawing.Image img = System.Drawing.Image.FromStream(stream); string imgName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + suff; //图片名称 img.Save(pFileName + imgName);//保存 ms.Close(); string path = pFileName + imgName;//图片全路径 objResult.ResComm.ResState = PublicCommonDML.ResFlag.Success; objResult.img_url = pImgPath+imgName;//返回图片相对路径 objResult.ResComm.ResMessage = "上传成功"; } return objResult; } catch (Exception ex) { string strMsg; string strErrMsg; strMsg = pUserInfo.User_ID + "\r\n" + pUserInfo.User_PWD + "\r\n" + ACCPCWS.MyCommons.Common.DMLToXML(pImgName.GetType(), pImgName); strErrMsg = LogLogic.WriteLog(ex, "E100008", strMsg); if (strErrMsg == "") { objResult.ResComm.ResState = PublicCommonDML.ResFlag.Error; objResult.ResComm.ResMessage = "系统服务器维护中,请稍后重试。"; } else { objResult.ResComm.ResState = PublicCommonDML.ResFlag.SysErrorMsg; objResult.ResComm.ResMessage = strErrMsg; } } return objResult; } #endregion