The following is a snippet of HttpPosts code for a single file in WebAPI. I would like to expand it to create a StreamContent containing multiple files (similar to Fiddler multi-user posts).
I know that I have to add a “border” to StreamContent, but I don’t know exactly where. I would like to eventually convert the FileStream / Stream parameters to a list so that I can iterate through the collection and create a StreamContent for POST.
Let me know if this message makes sense. I would 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 have no problem getting and saving published files. The problem is creating the StreamContent needed to publish multiple files.