The code snippet below successfully HttpPosts a single file to WebAPI. I'd like expand it to build StreamContent containing multiple files (similar to Fiddler multi-file posts).
I know I should be adding a "boundary" to the StreamContent, but I'm not sure exactly where. I'd like to eventually convert the FileStream/Stream parameters to be a List so I can iterate through the collection and build StreamContent to POST.
Let me know if this post makes any sense. I'd appreciate any suggestions.
Thanks in advance!
public async Task<HttpStatusCode> UploadOrderFile(FileStream imageFileStream, string filename, string contentType = "image/png")
{
JsonApiClient._client.DefaultRequestHeaders.Clear();
var content = new MultipartFormDataContent
{
JsonApiClient.CreateFileContent(imageFileStream, filename, contentType)
};
JsonApiClient._client.DefaultRequestHeaders.Add("Authorization",
" Bearer " + JsonApiClient.Token.AccessToken);
var response = await JsonApiClient._client.PostAsync("api/UploadFile", content);
response.EnsureSuccessStatusCode();
return response.StatusCode;
}
internal static StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"files\"",
FileName = "\"" + fileName + "\""
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return fileContent;
}
EDIT: I do not have any issues receiving and saving the posted files. The issue lies in creating the StreamContent necessary to post multiple files.