Exemplo n.º 1
0
        public async Task <IActionResult> EditMessage(MessageModel um, int id)
        {
            string         mess;
            MessageInBytes ms = new MessageInBytes {
                Text = um.Text
            };

            if (um.File != null)
            {
                byte[] data = null;
                // считываем переданный файл в массив байтов
                using (var binaryReader = new BinaryReader(um.File.OpenReadStream()))
                {
                    data = binaryReader.ReadBytes((int)um.File.Length);
                }
                // установка массива байтов
                ms.Content     = data;
                ms.Filename    = um.File.FileName;
                ms.TypeContent = um.File.ContentType;
            }
            else
            {
                ms.TypeContent = "Text";
            }

            using (var client = new HttpClient())
            {
                var         jsonModel = JsonConvert.SerializeObject(ms);
                HttpContent content   = new StringContent(jsonModel);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var response = await client.PutAsync("https://localhost:44330/api/messages/" + id, content);

                if (response.IsSuccessStatusCode)
                {
                    mess = "Message was changed";
                }
                else
                {
                    string textResult = await response.Content.ReadAsStringAsync();

                    mess = "Message wasnt changed. " + textResult;
                }
            }
            HttpContext.Session.SetString("mes", mess);

            return(RedirectToAction("EditMessage", "Home", new { id = id }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddMessage(MessageModel um)
        {
            MessageInBytes ms = new MessageInBytes {
                Text = um.Text, Date = DateTime.Now, UserId = HttpContext.Session.GetInt32("int") ?? 0
            };

            if (um.File != null)
            {
                byte[] data = null;
                // считываем переданный файл в массив байтов
                using (var binaryReader = new BinaryReader(um.File.OpenReadStream()))
                {
                    data = binaryReader.ReadBytes((int)um.File.Length);
                }
                // установка массива байтов
                ms.Content     = data;
                ms.Filename    = um.File.FileName;
                ms.TypeContent = um.File.ContentType;
            }
            else
            {
                ms.TypeContent = "Text";
            }
            HttpClient  client    = new HttpClient();
            var         jsonModel = JsonConvert.SerializeObject(ms);
            HttpContent content   = new StringContent(jsonModel);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = await client.PostAsync("https://localhost:44330/api/messages", content);

            if (response.IsSuccessStatusCode)
            {
                ViewBag.SendMessage = "Message was sent";
            }
            else
            {
                ViewBag.SendMessage = "Message wasnt sent. Unsupported type of file";
            }
            return(View());
        }