Example #1
0
        public async Task <HttpResponseMessage> UploadPersonDocument(string personId, string activityId = null)
        {
            Person person = await _mgrFcc.GetPerson(personId);

            if (person == null)
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

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

                foreach (var file in provider.Contents)
                {
                    if (!ContentTypeHelper.IsSupported(file.Headers.ContentType.ToString()))
                    {
                        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                    }

                    FileContent newFile = new FileContent();

                    var dataStream = await file.ReadAsStreamAsync();

                    var    ctnLength = Convert.ToInt32(file.Headers.ContentLength);
                    byte[] buffer    = new byte[ctnLength];
                    while (dataStream.Read(buffer, 0, ctnLength) > 0)
                    {
                        newFile.Id            = Guid.NewGuid().ToString();
                        newFile.BinaryContent = buffer;
                        newFile.FileType      = file.Headers.ContentType.ToString();
                        newFile.Name          = file.Headers.ContentDisposition.FileName.Replace("\"", "");
                        newFile.DateModified  = DateTime.Now;
                    }

                    string result = await _mgrFcc.SetPersonDocument(personId, newFile, activityId);

                    if (string.IsNullOrWhiteSpace(result))
                    {
                        throw new HttpResponseException(HttpStatusCode.InternalServerError);
                    }
                }

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