前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >webapi批量上传照片(三)(base64)

webapi批量上传照片(三)(base64)

作者头像
yaphetsfang
发布2020-07-30 10:09:59
1.1K0
发布2020-07-30 10:09:59
举报
文章被收录于专栏:yaphetsfangyaphetsfang
代码语言:javascript
复制
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);
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-11-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档