If you upload the file to the web API and want to access the file data ( Content-Disposition ), you must upload the file as a MIME multipart ( multipart/form-data ).
Here I showed some examples of how to load from HTML form, Javascript and .NET.
Then you can do something like this, this example only checks pdf / doc files:
public async Task<HttpResponseMessage> Post() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted - not multipart.")); } var provider = new RestrictiveMultipartMemoryStreamProvider(); //READ CONTENTS OF REQUEST TO MEMORY WITHOUT FLUSHING TO DISK await Request.Content.ReadAsMultipartAsync(provider); foreach (HttpContent ctnt in provider.Contents) { //now read individual part into STREAM var stream = await ctnt.ReadAsStreamAsync(); if (stream.Length != 0) { using (var ms = new MemoryStream()) { //do something with the file memorystream } } } return Request.CreateResponse(HttpStatusCode.OK); } } public class RestrictiveMultipartMemoryStreamProvider : MultipartMemoryStreamProvider { public override Stream GetStream(HttpContent parent, HttpContentHeaders headers) { var extensions = new[] {"pdf", "doc"}; var filename = headers.ContentDisposition.FileName.Replace("\"", string.Empty); if (filename.IndexOf('.') < 0) return Stream.Null; var extension = filename.Split('.').Last(); return extensions.Any(i => i.Equals(extension, StringComparison.InvariantCultureIgnoreCase)) ? base.GetStream(parent, headers) : Stream.Null; } }
Filip w
source share