public FileDownloadAgentWithMediaIdResult DownloadAgentWithMediaId(FileDownloadAgentWithMediaIdArgs args) { FileDownloadAgentWithMediaIdResult result; HttpRequestArgs requestArgs = new HttpRequestArgs(); requestArgs.Method = "POST"; requestArgs.Url = _fileServiceUri + "File/DownloadAgentWithMediaId"; requestArgs.Content = JsonConvert.SerializeObject(args); _log.Write("发起文件代理下载请求(DownloadAgentWithMediaId)", requestArgs.Content, TraceEventType.Verbose); HttpRequestResult requestResult = _httpService.Request(requestArgs); if (requestResult.Success) { _log.Write("文件代理下载请求返回(DownloadAgentWithMediaId)", requestResult.Content, TraceEventType.Verbose); result = JsonConvert.DeserializeObject <FileDownloadAgentWithMediaIdResult>(requestResult.Content); } else { result = new FileDownloadAgentWithMediaIdResult(); result.Message = requestResult.Exception.Message; } return(result); }
internal FileDownloadAgentWithMediaIdResult DownloadWithMediaId(DomainContext domainContext, FileDownloadAgentWithMediaIdArgs args) { //下载多媒体文件 //http://mp.weixin.qq.com/wiki/12/58bfcfabbd501c7cd77c19bd9cfa8354.html if (args.Sync) { return(DownloadFileWithMediaId(domainContext, args)); } else { object[] stateArray = new object[2]; stateArray[0] = domainContext; stateArray[1] = args; Task <FileDownloadAgentWithMediaIdResult> downloadTask = new Task <FileDownloadAgentWithMediaIdResult>(DownloadFileWithMediaIdTask, stateArray); downloadTask.ContinueWith((task) => { FileDownloadAgentWithMediaIdResult downloadResult; if (task.IsFaulted) { downloadResult = new FileDownloadAgentWithMediaIdResult(); if (task.Exception.InnerException != null) { downloadResult.Message = task.Exception.InnerException.Message; _log.Write("异步下载失败", task.Exception.InnerException.Message + "\r\n" + task.Exception.InnerException.StackTrace, TraceEventType.Error); } else { downloadResult.Message = task.Exception.Message; _log.Write("异步下载失败", task.Exception.Message + "\r\n" + task.Exception.StackTrace, TraceEventType.Error); } } else { downloadResult = task.Result; } //把下载结果POST到回调地址上 HttpRequestArgs requestArgs = new HttpRequestArgs(); requestArgs.Method = "POST"; requestArgs.Url = args.CallbackUrl; requestArgs.Content = JsonConvert.SerializeObject(downloadResult); _httpService.Request(requestArgs); }); downloadTask.Start(); FileDownloadAgentWithMediaIdResult result = new FileDownloadAgentWithMediaIdResult(); result.Message = "这是一个异步下载任务。"; return(result); } }
private FileDownloadAgentWithMediaIdResult DownloadFileWithMediaIdTask(object state) { object[] stateArray = (object[])state; DomainContext domainContext = (DomainContext)stateArray[0]; FileDownloadAgentWithMediaIdArgs args = (FileDownloadAgentWithMediaIdArgs)stateArray[1]; FileDownloadAgentWithMediaIdResult result = DownloadFileWithMediaId(domainContext, args); return(result); }
/// <summary> /// 提交投票项目 /// </summary> /// <returns></returns> public ActionResult CreatePictureVoteItem() { CreatePictureVoteItemArgs args = RequestArgs <CreatePictureVoteItemArgs>(); if (args == null) { return(RespondResult(false, "参数无效。")); } if (String.IsNullOrEmpty(args.ImageServerId)) { return(RespondResult(false, "参数无效。")); } //判断参与人数是否达到限制 if (_campaignManager.PictureVote.PictureVoteIsFullParticipant(args.CampaignId)) { return(RespondResult(false, "该活动已达最大允许参与人数。")); } //调用文件服务器进行代理下载,把上传到微信后台的文件下载下来 FileDownloadAgentWithMediaIdArgs downloadArgs = new FileDownloadAgentWithMediaIdArgs(); downloadArgs.Domain = DomainContext.Domain.Id; downloadArgs.MediaId = args.ImageServerId; FileDownloadAgentWithMediaIdResult downloadResult = _fileService.DownloadAgentWithMediaId(downloadArgs); string imageUrl; if (downloadResult.Success) { _log.Write("下载媒体文件返回:", JsonConvert.SerializeObject(downloadResult), TraceEventType.Verbose); imageUrl = _fileService.FileServiceUri + downloadResult.OutputFile; } else { return(RespondResult(false, "照片上传失败:" + downloadResult.Message)); } Campaign_PictureVoteItemEntity item = new Campaign_PictureVoteItemEntity(); item.Domain = DomainContext.Domain.Id; item.CampaignId = args.CampaignId; item.Member = MemberContext.Member.Id; item.Title = args.Title; item.Description = args.Description; item.Url = imageUrl; item.UploadTime = DateTime.Now; EnumCampaignCreatePictureVoteItemResult result = _campaignManager.PictureVote.CreatePictureVoteItem(item); return(RespondDataResult(result)); }
/// <summary> /// 下载代理 /// 将指定MediaId的文件下载到地文件服务器 /// </summary> /// <returns></returns> public ActionResult DownloadAgentWithMediaId() { FileDownloadAgentWithMediaIdArgs args = RequestArgs <FileDownloadAgentWithMediaIdArgs>(); if (args == null) { return(RespondResult(false, "参数无效。")); } args.ServerRootDir = Server.MapPath("/"); DomainContext domainContext = _fileDomainPool.GetDomainContext(args.Domain); FileDownloadAgentWithMediaIdResult result = _fileManager.DownloadWithMediaId(domainContext, args); return(new ContentResult() { Content = JsonConvert.SerializeObject(result) }); }
private FileDownloadAgentWithMediaIdResult DownloadFileWithMediaId(DomainContext domainContext, FileDownloadAgentWithMediaIdArgs args) { _log.Write("发起文件代理下载请求(WithMediaId)", JsonConvert.SerializeObject(args), TraceEventType.Verbose); string targetDir = Path.Combine(args.ServerRootDir, "FileStore", args.Domain.ToString()); if (Directory.Exists(targetDir) == false) { Directory.CreateDirectory(targetDir); } string accessToken = domainContext.AccessToken; HttpDownloadArgs downloadArgs = new HttpDownloadArgs(); downloadArgs.Url = String.Format( "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", accessToken, args.MediaId); downloadArgs.TargetDir = targetDir; HttpDownloadResult downloadResult = _httpService.Download(downloadArgs); FileDownloadAgentWithMediaIdResult result = new FileDownloadAgentWithMediaIdResult(); result.Success = downloadResult.Success; result.Message = downloadResult.Message; result.OutputFile = String.Format("FileStore/{0}/{1}", args.Domain, downloadResult.StoreFileName); result.ContentType = downloadResult.ContentType; result.Tag = args.Tag; result.MediaId = args.MediaId; _log.Write("文件代理下载请求返回(WithMediaId)", JsonConvert.SerializeObject(result), TraceEventType.Verbose); return(result); }