I have a class I created called Event. My Event class contains a list of Documents. Document is another class I've created. Each Document class contains a class called DocumentData which has a property called FileData of type byte[].
So it looks like this:
myEventObj.Documents[0].DocumentData.FileData
Right now my controller that accepts new or updated Events only accepts json data, which is fine for everything in my Event class except for FileData.
I have another controller which accepts file uploads. The problem is, there's no easy way for me to link the binary file data accepted by my second controller and pair it with the FileData property of an Event sent to my first controller. How do I combine them so there's only one call to my web service to save both an Event along with any file data?
Here's my Post and Put functions of my Event controller:
[HttpPost]
public IHttpActionResult Post(Event coreEvent) {
coreEvent.PrepareToPersist();
_unitOfWork.Events.Add(coreEvent);
_unitOfWork.Complete();
return Created("api/events/" + coreEvent.EventId, coreEvent);
}
[HttpPut]
public IHttpActionResult Put(Event coreEvent) {
coreEvent.PrepareToPersist();
_unitOfWork.Events.Update(coreEvent);
_unitOfWork.Complete();
return Ok(coreEvent);
}
Here's the Post function for my controller that handles file uploads:
[HttpPost]
public async Task<IHttpActionResult> Post() {
if (!Request.Content.IsMimeMultipartContent()) {
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var provider = new MultipartMemoryStreamProvider();
try {
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var file in provider.Contents) {
string filename = file.Headers.ContentDisposition.FileName.Trim('\"');
byte[] buffer = await file.ReadAsByteArrayAsync();
}
return Ok();
}
catch (System.Exception e) {
return InternalServerError(e);
}
}
How do I put these together?