webapi批量上传照片(三)(base64)
public HttpResponseMessage AddVisitNew(HttpRequestMessage request, CustomerVisitAddNewModel model) { return CreateHttpPostResponse<long>(request, () => { var res = customerVisitInfoService.AddVisitNew(model, CurrentUserId); if (string.IsNullOrEmpty(res.Error)) { // 上传拜访记录照片 string visitPath = null; FtpClient ftpClient = new FtpClient(GlobalConst.FtpAddress, GlobalConst.Username, GlobalConst.Password); string visitUploadDir = Path.Combine(GlobalConst.FILE_VISIT, "file"); for (var i = 0; i < model.VisitPic.Count; i++) { //获取base64编码的图片 HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; //获取文件储存路径 string path = context.Request.MapPath("~/"); //获取当前项目所在目录 string fName = "{0}".Fmt(DateTime.Now.ToString("yyyyMMddHHmmssfff")) + i; string suffix = ".jpg"; string strPath = path + "App_Data/" + fName + suffix; string newFileName = fName + suffix; //获取图片并保存 Base64ToImg(model.VisitPic[i].Split(',')[1]).Save(strPath); //上传FTP string uploadFilePath = Path.Combine(visitUploadDir, newFileName); visitPath = ftpClient.UploadFile(strPath, uploadFilePath, true); //FTP 转 HTTP visitPath = "{0}".Fmt(UriHelper.GetAbsolutePath(visitPath)); var pic = new VisitPiction { CustomerVisitInfoId = res.Data, Createtime = DateTime.Now, Path = visitPath }; customerVisitInfoService.AddVisitPic(pic); } Logger.Info("visits/add customer: " + model.CustomerId + ", VisitStatus: " + model.VisitStatus + ", ReVisitPlanTime: " + model.ReVisitPlanTime + ", ExpenrienceTime: " + model.ExpenrienceTime); } return res; }); } //解析base64编码获取图片 private Bitmap Base64ToImg(string base64Code) { MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Code)); return new Bitmap(stream); }