主题: 发布ASP.NET MVC KindEditor上传图片方法 |
作者: upupto, 发布日期: 2010-08-24 14:08:56, 浏览数: 11792 |
在Action中添加方法 [HttpPost] 125 public ActionResult UploadImage() 126 { 127 string savePath = "../Content/Images/"; 128 string saveUrl = "http://www.cnblogs.com/Content/Images/"; 129 string fileTypes = "gif,jpg,jpeg,png,bmp"; 130 int maxSize = 1000000; 131 132 Hashtable hash = new Hashtable(); 133 134 HttpPostedFileBase file = Request.Files["imgFile"]; 135 if (file == null) 136 { 137 hash = new Hashtable(); 138 hash["error"] = 0; 139 hash["url"] = "请选择文件"; 140 return Json(hash); 141 } 142 143 string dirPath = Server.MapPath(savePath); 144 if (!Directory.Exists(dirPath)) 145 { 146 hash = new Hashtable(); 147 hash["error"] = 0; 148 hash["url"] = "上传目录不存在"; 149 return Json(hash); 150 } 151 152 string fileName = file.FileName; 153 string fileExt = Path.GetExtension(fileName).ToLower(); 154 155 ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(',')); 156 157 if (file.InputStream == null || file.InputStream.Length > maxSize) 158 { 159 hash = new Hashtable(); 160 hash["error"] = 0; 161 hash["url"] = "上传文件大小超过限制"; 162 return Json(hash); 163 } 164 165 if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1) 166 { 167 hash = new Hashtable(); 168 hash["error"] = 0; 169 hash["url"] = "上传文件扩展名是不允许的扩展名"; 170 return Json(hash); 171 } 172 173 string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt; 174 string filePath = dirPath + newFileName; 175 file.SaveAs(filePath); 176 string fileUrl = saveUrl + newFileName; 177 178 hash = new Hashtable(); 179 hash["error"] = 0; 180 hash["url"] = fileUrl; 181 182 return Json(hash, "text/html;charset=UTF-8");; 183 } 页面中配置为: KE.show({ 关键还是在于 Json的格式
CNBLOG:http://www.cnblogs.com/upupto/archive/2010/08/24/1807202.html |
作者: Cerls, 发布日期: 2010-12-24 22:52:10 |
方法可用,不过我已经修改过代码了! 非常感谢! |
回复 |