1

i'd like to send string parameters and file to webserver. i have an error with streamContent This's my code

using (var client = new HttpClient())
{
  using (var content = new MultipartFormDataContent())
  {
   content.Add(new StringContent("loginTest"), "login");
   content.Add(new StringContent("toto"), "username");
   using (fileStream = new FileStream("C:\\test.pdf", FileMode.Open, 
          FileAccess.Read, FileShare.Read, 4096, useAsync: true))
    {
     var filestreamContent = new StreamContent(fileStream, 4096);
     content.Add(filestreamContent, "documentUpload", "test.pdf");

     using (var message = client.PostAsync("http://serverUrl/Create? 
      method=init", content))
     {
      var input = message.Result.Content.ReadAsStringAsync().Result;
      }
    }
   }
 }

and this is a screenshoot with error: System.ObjectDisposedException https://i.stack.imgur.com/Vt0Df.jpg

matoui
  • 21
  • 4

2 Answers2

1

When passing fileStream as a parameter to the constructor of StreamContent, it takes ownership. I.e. when disposing of filestreamContent, it will also dispose of fileStream. When adding filestreamContent to content, it also takes ownership, i.e. when disposing of content, it will loop through all the parts and then dispose those as well.

And HttpClient.PostAsync will in the end call the private function HttpClient.DisposeRequestContent, which in turn disposes content, which will close and dispose all associated streams. This is by design.

You're just seeing the end result, and as far as I can tell, you are not getting any errors or exceptions, besides when trying to access the members of the now disposed objects in the debugger.

Alexander Gräf
  • 511
  • 1
  • 3
  • 10
0

you can use this .

using (var httpClient = new HttpClient())
  {
   MemoryStream img = new MemoryStream();
   imgFile.InputStream.CopyTo(img1);
   img1.Seek(0, SeekOrigin.Begin);
   Dictionary<string, string> parameters = new Dictionary<string, string>();

   parameters.Add("Token", model.Token);
   parameters.Add("Phone", model.Phone);
   parameters.Add("FullName", model.FullName);
   
                  
   using (var content = new MultipartFormDataContent())
     {
      content.Add(new StreamContent(img1), "Image", "Image." + Path.GetExtension(national.FileName));
      HttpContent dictionaryItems = new FormUrlEncodedContent(parameters);
      content.Add(dictionaryItems, "AuthenticationViewModel");
                       
      var url = "mysite/api/People/InsertInfo";

      var response = await httpClient.PostAsync(url, content);
      var responseModel =
      JsonConvert.DeserializeObject<ResponseModel>(
                                (await response.Content.ReadAsStringAsync()));
     if (responseModel.IsSuccess)
                            return Json(new { IsSuccess = true }, JsonRequestBehavior.AllowGet);
     }

    }

And in Api

 var request = HttpContext.Current.Request;
 bool files = (request.Files.Count != 0);

 string data = request.Form.GetValues("AuthenticationViewModel").First();
 NameValueCollection model = HttpUtility.ParseQueryString(data);
 var token = model.GetValues("Token").First()
ar.gorgin
  • 4,765
  • 12
  • 61
  • 100