ASP.Net Web API

Topics: ASP.NET Web API
Dec 19, 2012 at 10:10 AM

Hi 

I have a problem getting file uploads working in asp.net web api I need to get multiple files uploaded from a web page. I have found how to do this in .net framework 4.5 using either MultipartMemoryStreamProvider or MultipartFormDataStreamProvider.

My solution needs to work on azure so avoiding the file system is highly preferable so I really need to use MultipartMemoryStreamProvider I also need to be able to get form data because I need to save the files in the database using a GUID provided from the client web form this is because a secondary request will be sent from the client to a different application with the same GUID which I can then use this GUID to tie up both requests together.

I can get the GUID from a hidden field if I use MultipartFormDataStreamProvider but not if I use MultipartMemoryStreamProvider here is my code

        [HttpPost]
        public async Task<HttpResponseMessage> Post()
        {

            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = new MultipartMemoryStreamProvider();

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                foreach (var item in provider.Contents)
                {
                    if (item.Headers.ContentDisposition.FileName != null)
                    {

                    }
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (System.Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }
        }

Strangley when I upload files using memory stream I can see the control in 
item.Headers.ContentDisposition.Parameters
But cannot get to its value

This is my first time using asp.net web api and I have never used MVC 

Any help appreciated 
Thanks 

Dec 21, 2012 at 10:44 AM

Ok I have solved this now I did not realise I still had access to HttpContext.Current.Request