js控件Kindeditor实现图片自动上传功能(2)

public class uploadpic : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string pic = context.Request.QueryString["pic"]; string[] arr = pic.Split('|'); string sstr = ""; UpLoadIMG st = new UpLoadIMG(); for (int i = 0; i < arr.Length; i++) { if (arr[i].IndexOf("http://") >= 0 || arr[i].IndexOf("https://") >= 0) { string std = st.SaveUrlPics(arr[i], "../../uploads/image/"); if (std.Length > 0) { if (i == arr.Length - 1) sstr += std; else sstr += std + "|"; } } } context.Response.Write(sstr); } public bool IsReusable { get { return false; } } } public class UpLoadIMG { public string SaveUrlPics(string imgurlAry, string path) { string strHTML = ""; string dirPath = HttpContext.Current.Server.MapPath(path); try { if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); dirPath += ymd + "https://www.jb51.net/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + imgurlAry.Substring(imgurlAry.LastIndexOf(".")); WebClient wc = new WebClient(); wc.DownloadFile(imgurlAry, dirPath + newFileName); strHTML = ymd + "https://www.jb51.net/" + newFileName; } catch (Exception ex) { //return ex.Message; } return strHTML; } }

  远程图片通过WebClient方法下载到服务器的相对路径"/uploads/image/"中,并且会按照日期自动生成文件夹和对应的文件名。返回的结果中包含了以"|"分隔的所有图片的本地相对地址,在步骤2的auto.js文件的uploadpic()函数中,回调方法success获取到该值并进行解析,将地址赋值给对应图片的src属性。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzsxxf.html