微信端上传图片/返回图片地址
这是从前端接收的base64的图片,流程:前端点击input 中 type=“file” 的上传,选中一张图片后随即运行ajax 走到后台,后台
#region 微信端的上传图片 [HttpPost] public ActionResult SaveNewsBackImage(string path, string ClassID, string TeacherID) { //保存图片到服务器, string basePath = "/Upload/Article/"; string imgname = DateTime.Now.Ticks + ".jpg"; string basepath = HttpContext.Server.MapPath(basePath); string Pic_Path = basepath + imgname; using (FileStream fs = new FileStream(Pic_Path, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fs)) { if (path.Contains(',')) path = path.Split(',')[1]; byte[] data = Convert.FromBase64String(path); bw.Write(data); bw.Close(); } } string serpath = ConfigurationManager.AppSettings["Domain"].ToString();//这是配置的是网站例:www.myaoang.com string classimagePath = serpath + basePath + imgname; return Content(classimagePath);//返回的是:www.myaoang.com/Upload/Artice/1234567890.jpg //把图片地址存到数据库 //string serpath = ConfigurationManager.AppSettings["Domain"].ToString(); //string classimagePath = serpath + basePath + imgname; //var flag = new ClassBLL().SaveNewsBackImage(classimagePath, Convert.ToInt32(ClassID), Convert.ToInt32(TeacherID)); //return Content("上传成功"); } #endregion