当前位置: 首页>>代码示例>>C#>>正文


C# MultipartFormDataContent.ReadAsStreamAsync方法代码示例

本文整理汇总了C#中System.Net.Http.MultipartFormDataContent.ReadAsStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# MultipartFormDataContent.ReadAsStreamAsync方法的具体用法?C# MultipartFormDataContent.ReadAsStreamAsync怎么用?C# MultipartFormDataContent.ReadAsStreamAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.MultipartFormDataContent的用法示例。


在下文中一共展示了MultipartFormDataContent.ReadAsStreamAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SendMultimedia

        /// <summary>
        /// Use this method to send multimedia files. On success, the sent Message is returned.
        /// </summary>
        /// <param name="chat_id">Unique identifier for the message recipient — User or GroupChat id.</param>
        /// <param name="file_path">File to send. You can upload a new file using multipart/form-data.</param>
        /// <param name="type">Type of file to send.</param>
        /// <param name="reply_markup">Additional interface options. A JSON-serialized object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user.</param>
        /// <param name="caption">Photo caption (only use with Type.Photo).</param>
        /// <param name="reply_to_message_id">If the message is a reply, ID of the original message.</param>
        /// <returns></returns>
        public Message SendMultimedia(int chat_id, string file_path, MultimediaType type, ReplyKeyboardHide reply_markup, string caption = null, int reply_to_message_id = -1)
        {
            string url = BaseUrl + "send" + type.ToString();

            byte[] file = null;
            using (var content = new MultipartFormDataContent("-------BotAPIDotNET"))
            {
                content.Add(new StringContent(string.Format("{0}", chat_id)), "chat_id");
                var fileStream = File.Open(file_path, FileMode.Open, FileAccess.Read);
                content.Add(new StreamContent(fileStream), type.ToString().ToLower(), file_path.Replace("\\", "/").Split('/').LastOrDefault());
                if (!string.IsNullOrEmpty(caption) && type == MultimediaType.Photo)
                    content.Add(new StringContent(caption), "caption");
                if (reply_to_message_id != -1)
                    content.Add(new StringContent(string.Format("{0}", reply_to_message_id)), "reply_to_message_id");
                if (reply_markup != null)
                    content.Add(new StringContent(JsonConvert.SerializeObject(reply_markup, Formatting.None)), "reply_markup");

                Stream multipart = content.ReadAsStreamAsync().Result;
                file = new byte[multipart.Length];
                multipart.Seek(0, SeekOrigin.Begin);
                multipart.Read(file, 0, (int)multipart.Length);
            }

            var request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "multipart/form-data; boundary=\"-------BotAPIDotNET\"";
            request.Method = "POST";

            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(file, 0, file.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            return ParseResponseMessage(responseString);
        }
开发者ID:tchusami,项目名称:TelegramBotsDotNet,代码行数:46,代码来源:BotApi.cs


注:本文中的System.Net.Http.MultipartFormDataContent.ReadAsStreamAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。