将图像作为字节发送到Windows应用程序中的Web服务

时间:2017-07-10 07:49:28

标签: c# web-services uwp

我正在使用UWP应用程序,因为我需要上传图像,因为我正在调用Web服务来发布详细信息。我使用以下代码序列化图像。

                byte[] fileBytes = null;
            using (var stream = await file.OpenReadAsync())
            {
                fileBytes = new byte[stream.Size];
                using (var reader = new DataReader(stream))
                {
                    await reader.LoadAsync((uint)stream.Size);
                    reader.ReadBytes(fileBytes);
                }
            }
            docs.Document = fileBytes;
            docs.DocumentName = file.Name;
            docs.DocumentTypeOtherDescription = "ProfilePicture";
            var docsAsJson = JsonConvert.SerializeObject(docs);
            StringContent stringContent = new StringContent(docsAsJson, System.Text.Encoding.UTF8);
            ByteArrayContent byteContent = new ByteArrayContent(fileBytes);
            MultipartFormDataContent httpContent = new MultipartFormDataContent();
            httpContent.Add(byteContent, file.Name);
            httpContent.Add(stringContent);            
            using (var httpClient = new HttpClient())
            {
                var request = new HttpRequestMessage {Method = HttpMethod.Post};
                request.Headers.Add("authorization", App.TokenType + " " + App.AccessToken);
                request.RequestUri = new Uri(App.BaseUrl + "api/User/UploadUserDocument");
                request.Content = httpContent;

                request.Content.Headers.Add(@"Content-Length", fileBytes.Length.ToString());
                var response = httpClient.SendAsync(request).Result;
                var data = response.Content.ReadAsStringAsync();
                if (response.IsSuccessStatusCode)
                {

                }
            }

这是我的序列化图像的一方,在服务方面,它们反序列化图像并保存在数据库中。但我得到StatusCode:500,ReasonPhrase:'内部服务器错误'这个错误,请任何人帮我解决这个问题。 这是我的服务代码:

       public IHttpActionResult UpdateUserWithProfilePic(FormData userviewmodel)
    {
        var error = string.Empty;
        var userJson = new StringBuilder();
        foreach (var items in userviewmodel.Fields)
        {
            if (items.Name == "}")
            {
                if (!userJson.ToString().EndsWith(",")) continue;
                userJson.Remove(userJson.Length - 1, 1);
                userJson.Append("}");
            }
            else
                userJson.Append((items.Name.Replace("%22", "\"")) + ":" + items.Value);
        }
        var userView = JsonConvert.DeserializeObject<UserViewModel>(userJson.ToString());
        var result = UpdateUser(userView, error);
        if (result.ResultType != ResultType.Success) return daHttpActionResult(result.Result, result);
        if (userviewmodel.Files != null && userviewmodel.Files.Count > 0)
        {
            userView.ProfileDocument = new UserDocument
            {
                DocumentName = userviewmodel.Files[0].Name,
                Document = userviewmodel.Files[0].Value.Buffer,
                UserID = UserId,
                DocumentType = DocumentTypeEnum.ProfilePicture,
                DocumentTypeOtherDescription = userviewmodel.Files[0].Value.MediaType,
            };
        }
        return AdHttpActionResult(result.Result, result);
    }

谢谢&amp;问候, 克里斯蒂娜

0 个答案:

没有答案