Xamarin表单无法将文件(图像)从移动设备发送到http post服务器

时间:2018-02-15 14:37:07

标签: c# http xamarin xamarin.forms

在尝试发送图像文件时,我收到错误的请求错误,我需要在请求正文中将图像作为文件发送而不是byte [],出了什么问题?

这是我的功能

                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); 

和应保存图像的服务器代码`

        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);`

0 个答案:

没有答案