My question is similar to this one: webapi-file-uploading-without-writing-files-to-disk
However, this and most of the other similar questions stop at reading the file data. I have made that bit work fine, but I am also wanting to read the rest of the form data in the form which has other elements for the upload such as 'Title' etc. This comes from the solution in the aforementioned question:
var filesToReadProvider = await Request.Content.ReadAsMultipartAsync();
The filesToReadProvider is a collection of HTTPContent objects, so I tried:
List<HttpContent> uploadedstuff = filesToReadProvider.Contents.ToList();
Image image = new Image(); ; // The image object we will create
Stream filestream; // The file stream object to use with the image
foreach (var thing in uploadedstuff)
{
try
{
string name = thing.Headers.ContentDisposition.Name.Replace("\"", ""); // String is quoted "\""namestring"\"" so need it stripped out
List<NameValueHeaderValue> parameters = thing.Headers.ContentDisposition.Parameters.ToList();
if (name == "file")
{
image.LocalFileName = thing.Headers.ContentDisposition.FileName;
filestream = await thing.ReadAsStreamAsync();
}
if (name == "Title")
{
// vvv- this line causes an exception.
NameValueCollection titleData = await thing.ReadAsFormDataAsync();
}
}
catch (System.Exception e)
{
var message = "Something went wrong";
HttpResponseMessage err = new HttpResponseMessage() { StatusCode = HttpStatusCode.ExpectationFailed, ReasonPhrase = message };
return ResponseMessage(err);
}
}
Any ideas what I should be doing to get to eg: the 'Title' form data? I feel I am close, but may be taking the wrong approach? Many thanks.