In Preview 5 we provide an HTTP file upload feature. It allows browsers/clients to send MIME multipart/form-data requests which contain attachments and data to the server. On the server you can pull them out using HttpContent, access the collection of attachments
as well as the form data.
If you look at the sample code below which is from Preview 5 (in the Microsoft.Net.Http.Formatting.Scenarios.MimeMultipart tests) you can see how to use it. Basically you use the IsMimeMultipartContent extension method to check if the content is multipart.
Then you can enumerate through the parts by calling ReadAsMultipartAsync which returns a collection of parts.
/// This sample WCF service reads the contents of an HTML file upload and writes one or more body parts to a local file.
internal class FileService
[WebInvoke(UriTemplate = "", Method = "POST")]
public List<FileResult> UploadFile(HttpRequestMessage request)
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
// Create a stream provider for setting up output streams
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider();
// Read the MIME multipart content using the stream provider we just created.
var task = request.Content.ReadAsMultipartAsync(streamProvider);
IEnumerable<HttpContent> bodyparts = task.Result;
// The submitter field is the entity with a Content-Disposition header field with a "name" parameter with value "submitter"
if (!bodyparts.TryGetFormFieldValue("submitter", out submitter))
submitter = "unknown";
// Get list of local file names from stream provider
IDictionary<string, string> bodyPartFileNames = streamProvider.BodyPartFileNames;
// Create response
return bodyPartFileNames.Select(kv =>
FileInfo fileinfo = new FileInfo(kv.Value);
return new FileResult
FileName = kv.Key,
LocalPath = fileinfo.FullName,
LastModifiedTime = fileinfo.LastWriteTimeUtc,
Length = fileinfo.Length,
Submitter = submitter
You'll notice there is a MultiPartFormDataStreamProvider in that sample. The provider handles automatically persisting any files that are uploaded to disk. You can configure the provider as to the root path, otherwise by default it uses a temp folder.
You don't "need' the provider unless you want the files automatically to be written. For example if you are storing the files in an external source such as a document database (my build talk) then you can just access the stream off the HttpContent instances
that are returned from the HttpContent.ReadAsMultiPartAsync method.
Hope this helps