I want to upload a file and send along with the file some additional information, let\'s say a string foo and an int bar.
How would I write a ASP.NET WebAPI
You can extract multiple files and multiple attributes in this way:
public async Task Post()
{
Dictionary attributes = new Dictionary();
Dictionary files = new Dictionary();
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents)
{
if (file.Headers.ContentDisposition.FileName != null)
{
var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
var buffer = await file.ReadAsByteArrayAsync();
files.Add(filename, buffer);
} else
{
foreach(NameValueHeaderValue p in file.Headers.ContentDisposition.Parameters)
{
string name = p.Value;
if (name.StartsWith("\"") && name.EndsWith("\"")) name = name.Substring(1, name.Length - 2);
string value = await file.ReadAsStringAsync();
attributes.Add(name, value);
}
}
}
//Your code here
return new HttpResponseMessage(HttpStatusCode.OK);
}