Jed
Jed

Reputation: 1074

Display PDF file from C# to Angular2 view

I'm trying to display a PDF that was previously uploaded into the server. The PDF resides inside the App_Data folder. I want to fetch it using C# Web API 2 and display it in my Angular 2 frontend view.

C# code:

  HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest);
  string filename = "somefilename.pdf";
  byte[] buffer = new byte[0];
  MemoryStream memStream = new MemoryStream();


  if (filename != "") {
    PdfReader reader = new PdfReader(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath) + "/App_Data/Uploads/PDFs/" + filename);
    PdfStamper stamper = new PdfStamper(reader, memStream);

    response = Request.CreateResponse(HttpStatusCode.OK);
    buffer = memStream.ToArray();
    response.Content = new StreamContent(new MemoryStream(buffer));
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    response.Content.Headers.ContentLength = buffer.Length;
    ContentDispositionHeaderValue contentDisposition = null;
    if (ContentDispositionHeaderValue.TryParse("inline; filename=" + filename, out contentDisposition))
    {
      response.Content.Headers.ContentDisposition = contentDisposition;
    }
  }

  return response;

Angular 2 code:

this.documentsService.getFile()
                .subscribe((response: any) => {
                    let file = new Blob([response], { type: 'application/pdf' });
                    let url = URL.createObjectURL(file);
                    window.open(url);
                });

I think my Angular code is wrong. But nevertheless, my first goal here is to fetch it from the MVC side. Right now the file gets downloaded and it's corrupted or there's not fetch correctly. It always give me a 15bytes size PDF file, so I know there's something wrong. I'm using iTextSharp in my C# backend.

Thanks in advance!

Upvotes: 0

Views: 2275

Answers (1)

PSGuy
PSGuy

Reputation: 749

From my comment (providing an answer is easier for code):

if (filename != "") {
    var fs = System.IO.File.OpenRead(filename);
    _myDisposable = fs; // see further down in this answer.
    var response = Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StreamContent(fs);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    return response;
}

return response;

Edit: The code I posted above works for me. The only problem is disposing of the stream. The best way to probably do that is by creating a private IDisposable member on your controller class, and then adding a dispose override, sort of like this:

private IDisposable _myDisposable;
public override void Dispose(bool disposing)
{
    if (disposing && _myDisposable != null)
        _myDisposable.Dispose();
}

Please note that this assumes ASP.NET is done with the request (which seems reasonable, because it's disposing of your controller), which should be correct.

Upvotes: 3

Related Questions