如何从HttpResponseMessage发送/读取图像

时间:2019-06-13 17:30:11

标签: c# json xmlhttprequest httpresponse

我正在尝试使用HttpResponseMessage响应PostAsync发送包含图像的模型。

要创建呼叫,代码如下所示:

    //convert the file for upload
            StreamReader streamReader = new StreamReader(Request.Files[0].InputStream);
            var binaryReader = new BinaryReader(streamReader.BaseStream);
            imageArray = binaryReader.ReadBytes(Request.Files[0].ContentLength);
            imageModel.uploadImage.image = Convert.ToBase64String(imageArray);

            var jsonParams = Newtonsoft.Json.JsonConvert.SerializeObject(imageModel.uploadImage);
            var content = new StringContent(jsonParams, System.Text.Encoding.UTF8, "application/json");

            //submit the file 
            using (HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
            {
                Uri baseURI = new Uri(_ServiceLocationRoot);
                client.BaseAddress = baseURI;
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = client.PostAsync(getUploadImageAddress, content).Result;

我已验证创建的json确实包含我要发送的数据,但是在接收端,当我尝试使用以下方法从请求中读回json时:

var requestContent = Request.Content;
string jsonContent = requestContent.ReadAsStringAsync().Result;

我得到一个空值。没有什么帮助? 谢谢。

0 个答案:

没有答案