0

While trying to send an image file I get bad request error , I need to send the image as a file not byte [] in the request body, What is going wrong ?

here's my func

                string json = JsonConvert.SerializeObject(jsonObject);
            System.Diagnostics.Debug.WriteLine(TAG, "Sended Json " + json);
            var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            if (!string.IsNullOrEmpty(Token))
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
                System.Diagnostics.Debug.WriteLine(TAG, " Token is : " + Token);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(TAG, " Token is null ");
            }
            var contentJson = new StringContent(json, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(url, contentJson); 

and the server code where the image should be saved `

        var httpRequest = HttpContext.Current.Request;

        if (httpRequest.Files.Count > 0)

        {
            var ImagesFolder = HttpContext.Current.Server.MapPath("~/FileServer/MedicalEntity/" + MedicalEntityID.ToString() + "/RegImages/");
            string ImageName = "";

            string ImageExt = "";
            if (!Directory.Exists(ImagesFolder))
            {
                Directory.CreateDirectory(ImagesFolder);
            }

            var docfiles = new List<string>();

            foreach (string file in httpRequest.Files)

            {

                var postedFile = httpRequest.Files[file];
                ImageExt= postedFile.FileName.Split('.').Last();
                var filePath = ImagesFolder+ImageName+"."+ImageExt;

                postedFile.SaveAs(filePath);

                docfiles.Add(filePath);
                using (MedicalEntityFactory oMedFac=new MedicalEntityFactory())
                {
                    oMedFac.UpdateImageStatus(MedicalEntityID, ImageType);
                }

            }

            result = Request.CreateResponse(HttpStatusCode.Created, docfiles);`
Nassar
  • 100
  • 3

0 Answers0