Ejemplo n.º 1
0
        //ref: http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api

        public async Task<IEnumerable<FileDesc>> Post()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable,
                    "This request is not properly formatted"));
            }

            var streamProvider = new MultipartFormDataMemoryStreamProvider();
            return await Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
                MultipartFormDataMemoryStreamProvider resultado = t.Result;
                string projectId = resultado.FormData.GetValues("hidProjectId").Single();
                IEnumerable<FileDesc> fileInfo =
                    streamProvider.Contents.Where((content, idx) => resultado.IsStream(idx)).Select(i =>
                    {
                        string fileName = i.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
                        byte[] fileContent = i.ReadAsByteArrayAsync().Result;

                        return new FileDesc(fileName, projectId, i.Headers.ContentLength.Value/1024);
                    });

                return fileInfo;
            });
        }