Example #1
0
        /// <summary>
        /// 新增其他类型永久素材
        /// </summary>
        /// <param name="filePathName">指定包含完整路径的文件名</param>
        /// <param name="videoDescription">上传视频素材时,需提供视频的描述信息</param>
        /// <returns>新增永久素材的结果</returns>
        public WeChatResult <MaterialResult> AddMaterial(string filePathName, VideoDescription videoDescription = null)
        {
            string accessToken = connect.GetAccessToken();
            string type        = MimeMapping.GetMimeMapping(filePathName); //获取文件的Mime-Type

            type = type.Substring(0, type.IndexOf('/'));                   //提取Mime-Type的前部分
            string fileName = Path.GetFileName(filePathName);

            using (Stream fileStream = new FileStream(filePathName, FileMode.Open))
            {
                if ("image".Equals(type) && fileStream.Length < 64 * 1024)
                {
                    type = "thumb";//如果文件属于图片,而且小于64KB的,类型改成缩略图(thumb)
                }
                else if ("audio".Equals(type))
                {
                    type = "voice";
                }
                string          url       = $"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={accessToken}&type={type}";
                UploadFileParam fileParam = new UploadFileParam(url, fileName, fileStream);
                if (videoDescription != null)
                {
                    fileParam.PostParameters.Add("title", videoDescription.title);
                    fileParam.PostParameters.Add("introduction", videoDescription.introduction);
                }
                string resultStr = SimulateRequest.UploadFile(fileParam);
                WeChatResult <MaterialResult> weChatResult = new WeChatResult <MaterialResult>(resultStr);
                if (weChatResult.errcode != WeChatErrorCode.SUCCESS)
                {
                    SystemLogHelper.Warn(MethodBase.GetCurrentMethod(), $"上传图文消息内的图片获取URLGetUrlByUpdateImg,微信服务报错:{weChatResult}");
                }
                return(weChatResult);
            }
        }
Example #2
0
        /// <summary>
        /// 批量创建云图数据
        /// </summary>
        /// <param name="fileName">文件名,如:cloudmap.xlsx</param>
        /// <param name="batchData">云图数据批量参数</param>
        /// <returns>批量创建任务结果</returns>
        public CloudBatchDataResult CreateBatchData(string fileName, BatchDataParam batchData)
        {
            if (batchData == null)
            {
                throw new ArgumentNullException("batchData");
            }
            UploadFileParam uploadFileParam = new UploadFileParam("https://yuntuapi.amap.com/datamanage/data/batchcreate",
                                                                  fileName, batchData.file);

            uploadFileParam.FileNameKey    = "file";
            uploadFileParam.PostParameters = batchData.GenerateParams();
            string resultStr = WebRequestHelper.UploadFile(uploadFileParam);
            CloudBatchDataResult resultObj = JsonConvert.DeserializeObject <CloudBatchDataResult>(resultStr);

            return(resultObj);
        }
Example #3
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="parameter">包含文件二进制数据的上传参数</param>
        /// <returns>上传后的响应结果</returns>
        public static string UploadFile(UploadFileParam parameter)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // 1.分界线
                string boundary           = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")), // 分界线可以自定义参数
                       beginBoundary      = string.Format("--{0}\r\n", boundary),
                       endBoundary        = string.Format("\r\n--{0}--\r\n", boundary);
                byte[] beginBoundaryBytes = parameter.Encoding.GetBytes(beginBoundary),
                endBoundaryBytes = parameter.Encoding.GetBytes(endBoundary);
                // 2.组装开始分界线数据体 到内存流中
                memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length);
                // 3.组装 上传文件附加携带的参数 到内存流中
                if (parameter.PostParameters != null && parameter.PostParameters.Count > 0)
                {
                    foreach (KeyValuePair <string, string> keyValuePair in parameter.PostParameters)
                    {
                        string parameterHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n{2}", keyValuePair.Key, keyValuePair.Value, beginBoundary);
                        byte[] parameterHeaderBytes    = parameter.Encoding.GetBytes(parameterHeaderTemplate);

                        memoryStream.Write(parameterHeaderBytes, 0, parameterHeaderBytes.Length);
                    }
                }
                // 4.组装文件头数据体 到内存流中
                string fileHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n", parameter.FileNameKey, parameter.FileNameValue);
                byte[] fileHeaderBytes    = parameter.Encoding.GetBytes(fileHeaderTemplate);
                memoryStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
                // 5.组装文件流 到内存流中
                byte[] buffer = new byte[1024 * 1024 * 1];
                int    size   = parameter.FileStream.Read(buffer, 0, buffer.Length);
                while (size > 0)
                {
                    memoryStream.Write(buffer, 0, size);
                    size = parameter.FileStream.Read(buffer, 0, buffer.Length);
                }
                // 6.组装结束分界线数据体 到内存流中
                memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
                // 7.获取二进制数据
                byte[] postBytes = memoryStream.ToArray();
                // 8.HttpWebRequest 组装
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(parameter.Url, UriKind.RelativeOrAbsolute));
                webRequest.Method        = "POST";
                webRequest.Timeout       = int.MaxValue;
                webRequest.ContentType   = string.Format("multipart/form-data; boundary={0}", boundary);
                webRequest.ContentLength = postBytes.Length;
                if (Regex.IsMatch(parameter.Url, "^https://"))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                    ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
                }
                // 9.写入上传请求数据
                using (Stream requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(postBytes, 0, postBytes.Length);
                    requestStream.Close();
                }
                // 10.获取响应
                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), parameter.Encoding))
                    {
                        string body = reader.ReadToEnd();
                        reader.Close();
                        return(body);
                    }
                }
            }
        }