I want to upload multiple files at the same time, is the purpose of the incoming parameters body, below is the code, when small file upload will be a problem, but a large file or multi-threaded upload will be an error, do you know what reason be cause?
//the client code
Using (var stream=new FileStream (@ "e: \ \ upload test. PDF", FileMode. Open, FileAccess. Read))
{
Using (var client=new HttpClient ())
{
Using (var multiContent=new MultipartFormDataContent ())
{
Var bodyContent=new StringContent (Newtonsoft. Json. JsonConvert. SerializeObject (content), Encoding, UTF8);
BodyContent. Headers. ContentDisposition=new ContentDispositionHeaderValue (" body ");
Stream. The Position=0;
Var streamContent=new streamContent (stream);
StreamContent. Headers. ContentDisposition=new ContentDispositionHeaderValue (" attachment ");
//upload body
MultiContent. Add (bodyContent);
//upload file stream
MultiContent. Add (streamContent);
Var. MultiResult=client PostAsync (url, multiContent). The Result;
}
}
}
//the server code
[HttpPost]
Public HttpResponseMessage Test ()
{
Var provider=new MultipartMemoryStreamProvider ();
Task. Factory. StartNew (()=& gt; The provider=base. Request. Content. ReadAsMultipartAsync (). The Result, CancellationToken. None, TaskCreationOptions. LongRunning, TaskScheduler. (the Default). Wait ();
Foreach (var item in the provider. Contents)
{
//read the body parameter
If (item. Headers. ContentDisposition. DispositionType=="body")
{
}
//read the file flow
If (item. Headers. ContentDisposition. DispositionType=="attachment")
{
}
}
Return new HttpResponseMessage (System.Net.HttpStatusCode.OK);
}