asp.net core 3.1 HttpClient Error while copying content to a stream.

astheras 1 Reputation point
2020-12-10T08:08:20.82+00:00

we have old file server with post and get requests to work with files. Now we need to bind our web server with old file server. But i'm getting Error while copying content to a stream. when PostAsync. What i'm doing wrong?

[HttpPost]
    public async Task<string> UploadFile(string data) {
      dynamic execResult = null;
      TDecryptedCookie cookie = TAuthorization.GetCookieInfo(Request);
      dynamic json = TTools.Deserialize(data);


      string url = $"{TConst.FileServerHost}FileService/Upload?data={{ \"folder\":\"{json.F_Folder.Value}\", \"filename\":\"{json.F_FileName.Value}\",\"personID\":{cookie.PersonID}}}";


      dynamic result;
      var baseAddress = new Uri(url);

      try {
        using (var client = new HttpClient() { BaseAddress = baseAddress }) {
          client.DefaultRequestHeaders.Add("Authorization", Request.Headers["Authorization"].ToString()); 
          using var form = new MultipartFormDataContent();

          foreach (var file in Request.Form.Files) {
            //обработка файла
            byte[] fileBytes;

            using var ms = new MemoryStream();
            file.CopyTo(ms);
            fileBytes = ms.ToArray();
            using var fileContent = new ByteArrayContent(fileBytes);
            form.Add(fileContent, "file", file.FileName);
          }
          using var response = await client.PostAsync(baseAddress, form);   // <====== Error while copying content to a stream.
          response.EnsureSuccessStatusCode();
          var responseContent = await response.Content.ReadAsStringAsync();
          result = await TTools.Deserialize(responseContent);


      } catch (Exception e) {
        result = new { result = false, message = e.Message };
      }
      return result;
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,177 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. astheras 1 Reputation point
    2020-12-10T14:27:34.997+00:00

    resolved by reading innerexception not e.Message


  2. Nijil N 1 Reputation point
    2022-04-08T01:59:23.347+00:00

    Can you pls share the innerexception and the solution.

    0 comments No comments