Skip to content

Instantly share code, notes, and snippets.

@davidbreyer
Last active May 2, 2016 13:47
Show Gist options
  • Save davidbreyer/a7922060a39484d3e59341a8388e3bfa to your computer and use it in GitHub Desktop.
Save davidbreyer/a7922060a39484d3e59341a8388e3bfa to your computer and use it in GitHub Desktop.
Return a PDF to the browser in the form of a byte array.
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
public class FileServiceController : ApiController
{
[Route("api/fileservice/{docId}")]
public HttpResponseMessage Get(string docId)
{
byte[] response = FileProxy.GetDocumentStream(docId);
if (response == null) return new HttpResponseMessage(HttpStatusCode.BadRequest);
MemoryStream ms = new MemoryStream(response);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(ms.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment