/// <summary>
        /// 通过ServerID(mediaid)从微信服务器上下载图片,保存到本地,并返回文件名
        /// </summary>
        /// <param name="mediaid"></param>
        /// <returns></returns>
        public async Task <string> Get(string mediaid)
        {
            var mongo = new MongoDBHelper <WeixinImgFileModels>("weixinImgFile");

            //查询mongo中是否存储了mediaid对应的照片文件
            var doc = await mongo.SelectOneAsync(x => x.MediaId == mediaid);

            if (doc != null)
            {
                return(doc.FileName);
            }

            //如果文件没有下载过,则下载
            //http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
            var queryString = HttpUtility.ParseQueryString(string.Empty);

            queryString["access_token"] = await Get();

            queryString["media_id"] = mediaid;

            var uri = "http://file.api.weixin.qq.com/cgi-bin/media/get?" + queryString;

            HttpResponseMessage response;

            response = await client.GetAsync(uri);

            var msg = await response.Content.ReadAsStreamAsync();

            var fileName = response.Content.Headers.ContentDisposition.FileName.Replace("\"", "");

            var helper = new ProjecToxfordClientHelper();

            var content = await FileHelper.ReadAsync(msg);

            FileHelper.SaveFile(content, fileName);

            await mongo.InsertAsync(new WeixinImgFileModels()
            {
                FileName = fileName,
                MediaId  = mediaid
            });

            return(fileName);
        }
Esempio n. 2
0
 public FaceController()
 {
     client = new ProjecToxfordClientHelper();
 }