I have a sample code from one of the plugin I was trying.
HttpResponseMessage hrm = client.PostAsync(uri, content).GetAwaiter().GetResult();
if (hrm.StatusCode == HttpStatusCode.OK)
{
MultipartMemoryStreamProvider provider = hrm.Content.ReadAsMultipartAsync().GetAwaiter().GetResult();
if (provider.Contents.Count > 0)
{
for (int i = 0; i < provider.Contents.Count; i++)
{
HttpContent rcontent = provider.Contents[i];
byte[] bytes = rcontent.ReadAsByteArrayAsync().GetAwaiter().GetResult();
File.WriteAllBytes(@"c:\temp\ConvertToPDF" + i + ".pdf", bytes);
}
}
}
This code posts n number of files to an api, and get the same files in pdf format as a response.
Then the response is saved into different files. Right now, MultipartMemoryStreamProvider
has n number of files in the Contents
collection. But, both of those files are actually pointing to the last file that I selected.
Code to create the request
//Multipart content
MultipartContent content = new MultipartContent();
foreach (var singleFile in files)
{
//Create the StreamContent of the input file and add to the MultpartContent
StreamContent is1 = new StreamContent(singleFile.InputStream);
is1.Headers.ContentType = new MediaTypeHeaderValue(singleFile.ContentType);
is1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
is1.Headers.ContentDisposition.Name = singleFile.FileName;
is1.Headers.ContentDisposition.FileName = singleFile.FileName;
content.Add(is1);
}
This is the code that creates the content object. I checked the object and it actually contains two different files. So, I am unsure why this below line is causing an issue.
MultipartMemoryStreamProvider provider = hrm.Content.ReadAsMultipartAsync().GetAwaiter().GetResult();
P.S. Plugin name is ActivePDF DocConverter. Sadly it does not provide a hosted demo link, so I can't provide the url here. I am using my local machine to host that plugin.