Example #1
0
        public ActionResult ShowImage(string mediaId)
        {
            var smallFileName  = CustomerMessage.GetCacheSmallPathForTemporaryMedia(mediaId);
            var targetFileName = smallFileName;

            if (!System.IO.File.Exists(smallFileName))
            {                                         //如果没有小图,说明没进行缩小处理过
                var fileName = CustomerMessage.GetCachePathForTemporaryMedia(mediaId);
                if (!System.IO.File.Exists(fileName)) //如果没有原图,则需要下载下来
                {
                    var accessToken = GetAccessToken();
                    using (FileStream createFs = new FileStream(fileName, FileMode.Create))
                    {
                        MediaApi.Get(accessToken, mediaId, createFs);
                    }
                }
                try
                {
                    ResizeImage(fileName, smallFileName);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "缩放出错,文件名:{0}", fileName);
                    targetFileName = fileName; //显示的时候用原图显示。
                }
            }

            FileStream fs = new FileStream(targetFileName, FileMode.Open);

            return(File(fs, "image/jpeg"));
        }
Example #2
0
        public async Task <APIResult> SendCustomerImage([FromBody] SendCustomerImageArgsModel args)
        {
            if (string.IsNullOrEmpty(args.OpenId))
            {
                throw new ArgumentNullException("OpenId");
            }

            var user = GetUsername();

            wechatCoreDb.EnsureIsCustomerSessionWorker(args.OpenId, user);


            string requestStrValue      = args.Data.Substring(args.Data.IndexOf(',') + 1);                              //代表 图片 的base64编码数据
            string requestFileExtension = args.Data.Split(new char[] { ';' })[0].Substring(args.Data.IndexOf('/') + 1); //获取后缀名

            var buffer     = Convert.FromBase64String(requestStrValue);
            var folderPath = $"{Directory.GetCurrentDirectory()}/App_Data/Weixin/TemporaryMedia";

            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            var fileName = $"{folderPath}/{DateTime.Now.ToString("yyyyMMddHHhhss")}{CommonUtil.CreateNoncestr(6)}.{requestFileExtension}";

            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                fs.Write(buffer, 0, buffer.Length);
                ////通过代码审查说,使用了using,不需要再次调用close
                //fs.Close();
            }

            //fileName = @"C:\Xiang\Temp\1.jpg";
            var accessToken = GetAccessToken();
            //var uploadResult = Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadTemporaryMedia(accessToken, UploadMediaFileType.image, fileName);

            var uploadResult = await WechatMediaApiProxy.UploadAsync <Senparc.Weixin.MP.AdvancedAPIs.Media.UploadTemporaryMediaResult>(accessToken, "image", fileName);

            var data = new
            {
                msgType = "image",
                mediaId = uploadResult.media_id
            };

            System.IO.File.Move(fileName, CustomerMessage.GetCachePathForTemporaryMedia(data.mediaId));
            var msg = new CustomerMessage()
            {
                FromUser = GetUsername(),
                ToUser   = args.OpenId,
                Time     = DateTime.Now,
                Content  = Newtonsoft.Json.JsonConvert.SerializeObject(data),
                ChatFlag = args.OpenId
            };

            wechatCoreDb.AddToCustomerMessage(msg);
            wechatCoreDb.UpdateCustomerSessionTime(args.OpenId);

            wechatCoreDb.SaveChanges();
            return(Success());
        }