using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Collections; using System.Text; using SuperMap.RealEstate.HousingSecurity.APIPlugs; using SuperMap.RealEstate.HousingSecurity.APIPlugs.flashuploader; using System.Web.Script.Serialization; namespace SuperMap.RealEstate.HousingSecurity.API.upload { /// /// upimg 的摘要说明 /// 【提供给文章编辑的ueditor编辑器上传图片】 /// public class upimg : IHttpHandler { private static int _USER_ID = 1;//MOCK private static string _USER_NAME = "admin"; private const string SUCCESS = "SUCCESS"; private const string ERROR = "ERROR"; private HttpContext context; public void ProcessRequest(HttpContext context) { this.context = context; //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); FlashUpJsonInfo _json = UpHousePhotos(0); //直接输出json //context.Response.ContentType = "application/json"; //context.Response.Write("{"); //context.Response.Write("'state':'" + _json.state + "'"); //context.Response.Write(",'msg':'" + _json.msg + "'"); //context.Response.Write(",'title':'" + _json.title + "'"); //context.Response.Write(",'url':'" + _json.url + "'"); //context.Response.Write(",'original':'" + _json.original + "'"); //context.Response.Write(",'fid':" + _json.fid); //context.Response.Write("}"); context.Response.ContentType = "application/json"; Hashtable ht = new Hashtable(); ht.Add("state", _json.state); ht.Add("msg", _json.msg); ht.Add("title", _json.title); ht.Add("url", _json.url); ht.Add("original", _json.original); ht.Add("fid", _json.fid); StringBuilder buider = new StringBuilder(); JavaScriptSerializer json = new JavaScriptSerializer(new SimpleTypeResolver()); json.MaxJsonLength = Int32.MaxValue; json.Serialize(ht, buider); context.Response.Write(buider.ToString()); } #region [图片上传] -> UpHousePhotos <- 上传房源相关图片 public FlashUpJsonInfo UpHousePhotos(int houseid) { FlashUpJsonInfo _json = new FlashUpJsonInfo(); MyUpFileInfo myupfileinfo = null; if (doupload(_json, out myupfileinfo)) { //同时添加到房源图片目录 //try //{ // //using (Ctrl_photos _ctrl = new Ctrl_photos()) // //{ // // _ctrl.FieldValues.Add("fk_type", Ctrl_photos.FK_TYPE_HOUSE); // // _ctrl.FieldValues.Add("fk_id", houseid);//关联houseid // // _ctrl.FieldValues.Add("fid", myupfileinfo.Entity.fid); // // _ctrl.FieldValues.Add("imgurl", myupfileinfo.GetUri()); // // _ctrl.FieldValues.Add("imgtitle", myupfileinfo.Entity.memo); // // _ctrl.FieldValues.Add("add_userid", LoginHelper.Sess_UserId); // // _ctrl.FieldValues.Add("add_usernick", LoginHelper.Sess_UserNick); // // _ctrl.FieldValues.Add("add_ip", LoginHelper.Sess_UserId); // // _ctrl.FieldValues.Add("addtime", DateTime.Now); // // _ctrl.FieldValues.Add("edittime", DateTime.Now); // // _ctrl.Insert(); // //} //} //catch (Exception ex) //{ // _json.state = ERROR; // _json.msg = "upload file success,but save photos is error:" + ex.Message; //} } return _json; } #endregion private bool doupload(FlashUpJsonInfo _json, out MyUpFileInfo _upfileinfo) { bool _isok = false; _upfileinfo = null; HttpRequestBase request = new HttpRequestWrapper(this.context.Request); //Hashtable _ht = new Hashtable(); if (request.Files != null && request.Files.Count > 0) { //处理出需要上传的文件对象(单文件处理) _upfileinfo = new MyUpFileInfo(request.Files[0], _USER_ID, true);// 设定为新闻图片目录:isnews=true //[处理返回的参数] _upfileinfo.Entity.memo = GetFileFieldVal(context, "pictitle");//ueditor专用字段,用于标识title,并且记录到数据库 string _msg = string.Empty; if (UpFlashHelper.TrySaveUpFile(_upfileinfo.HttpPostedFileBase, _USER_ID, _USER_NAME, ref _upfileinfo, out _msg)) { _isok = true;//标记成功 _json.state = SUCCESS; _json.msg = _msg; _json.url = _upfileinfo.GetUri(); //_json.sUrl = _upfileinfo.GetUri(); _json.title = _upfileinfo.Entity.memo; _json.original = _upfileinfo.Entity.filename; _json.fid = _upfileinfo.Entity.fid; //if (currentType != null) // infoList.Add("currentType", currentType); //if (originalName != null) // infoList.Add("originalName", originalName); } else { _json.state = ERROR; _json.msg = _msg; //Zoyoa.Core.Utility.SecurityHelper.HtmlEncode(_msg) //显示信息需要编码,防止JSON解析错误无法显示信息 } } else { _json.state = ERROR; _json.msg = "Request.Files is null"; } return _isok; } private static string GetFileFieldVal(HttpContext cxt, string field) { string info = null; if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field])) { info = cxt.Request.Form[field].Trim(); } return info; } public bool IsReusable { get { return false; } } } }