1

I am able to download and store the pdf document that is being generated but instead of downloading I want to open it in browser. I have something like this

MemoryStream os = new MemoryStream();
PdfWriter writer = new PdfWriter(os);
var pdfDocument = new PdfDocument(writer);

using (var document = new Document(pdfDocument))
{
  //I am adding different sections here
}
var response = new HttpResponseMessage
{
    StatusCode = HttpStatusCode.OK,
    Content = new ByteArrayContent(os.ToArray())
};

response.Content.Headers.Add("Content-Type", "application/pdf");
response.Headers.Add("Content-disposition", "attachment;filename=" + "testPDF.pdf");
return response;

The response is further being sent to controller and there it is being downloaded but I want to open in new browser. For me, "Content-disposition", "attachment;filename" is not working.

My return value is being passed on the controller further where I am storing in blob and continues to download.

public async Task<IActionResult> GenerateDocument(int id)
    {
        var result = await _applicationService.GenerateDocument(id);
        var blobResult = await _applicationService.SaveDocument(id, result.ResponseObject);

        IActionResult OnSuccess() =>
            new RedirectResult(blobResult.ResponseObject.URI, true);

        return HandleResult(OnSuccess, blobResult.Status);
    }
Tina
  • 89
  • 1
  • 11
  • I know there are similar question but the thing is for me the solution they mentioned is not working ....I did mention that in my comment too. – Tina Sep 15 '17 at 16:22

1 Answers1

2

You would want to use inline for the content disposition to let the browser know to display it

//...code removed for brevity
var buffer = os.ToArray();
var contentLength = buffer.Length;
var statuscode = HttpStatusCode.OK;
var response = Request.CreateResponse(statuscode);
response.Content = new ByteArrayContent(buffer);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentLength = contentLength;
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("inline; filename=" + "testPDF.pdf", out contentDisposition)) {
    response.Content.Headers.ContentDisposition = contentDisposition;
}
return response;
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • I tried replacing the code but it continued to download.I dont mind the downloading part but I need it to open in browser. I am also thinking its something to do with controller. – Tina Sep 14 '17 at 14:22