File upload using MultipartMemoryStreamProvider RRS feed

  • Question

  • User475045581 posted

    I am looking for a way to implement file upload without writing to the file system.

    So far I have only seen samples (that work with RTM) that writes to the filesystem.

    What I would like is to create an implementation of MultipartMemoryStreamProvider where I have the stream(s) available to work with.

    Can anyone give any guidance on this? Any sample code?



    Tuesday, September 11, 2012 5:09 PM

All replies

  • User-837620913 posted

    Sample code from this full blog post on asynchronous file uploads in Web API. See the blog post for more details.

    public class UploadingController : ApiController
        public void Post()
            if (Request.Content.IsMimeMultipartContent())
                var streamProvider = new MultipartFormDataStreamProvider("c:/uploads/");
                var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
                        if (t.IsFaulted || t.IsCanceled)
                            throw new HttpResponseException(HttpStatusCode.InternalServerError);
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));

    Wednesday, September 12, 2012 6:35 AM
  • User1594254982 posted

    Hi Anders,

    MultipartMemoryStreamProvider reads the incoming multipart request, as the name says, into memory streams...following is an example:

    Request.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(new MultipartMemoryStreamProvider()).ContinueWith((tsk) =>
                            MultipartMemoryStreamProvider prvdr = tsk.Result;
                            foreach (HttpContent ctnt in prvdr.Contents)
                                // You would get hold of the inner memory stream here
                                Stream stream = ctnt.ReadAsStreamAsync().Result;
                                // do something witht his stream now

    hope this helps

    Wednesday, September 12, 2012 11:29 AM
  • User3112162 posted

    as I understand, we can use the same MultipartMemoryStreamProvider

    var streamProvider = new MultipartMemoryStreamProvider();
    .Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(streamProvider).ContinueWith((tsk) => { foreach (HttpContent ctnt in streamProvider.Contents) { // You would get hold of the inner memory stream here Stream stream = ctnt.ReadAsStreamAsync().Result; // do something witht his stream now } });
    Saturday, October 25, 2014 7:35 AM
  • User-1561511148 posted

    Hi kiran ,i'm new for asp.net web api please helpme. may i know how transfer image to this method from console or aspx.cs 

    this is my controller class

    public async Task<IHttpActionResult> PostYardLayout(int YardId)


    var provider = new MultipartMemoryStreamProvider();
    await Request.Content.ReadAsMultipartAsync(provider);


    i'm unable to know how image getting through MultipartMemoryStreamProvider.

    Friday, January 30, 2015 2:01 AM
  • User-1561511148 posted

    Hi any body knows how to Testing Asp.net WEB API MultipartStreamprovider file upload.


    Friday, January 30, 2015 2:07 AM
  • User2032108616 posted


    his link will help you to create a console application and test your end point.

    Thursday, August 10, 2017 1:33 PM