Daniel Rusnok
Daniel Rusnok

Reputation: 489

Content-Disposition: inline for PDF file in ASP.Net not working

I am trying to return PDF file to browser with header Content-Dispostion:inline right after I am creating this file. Viewers of browser have problem to open it.

File is not corrupted. If I put into browser, viewer shows file correctly. But I want create file and check users rights for file at one request.

Here is my headers and response set up:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline");
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
HttpContext.Current.Response.AddHeader("filename", $"objednavka-{OrderId}.pdf");
HttpContext.Current.Response.BinaryWrite(File.ReadAllBytes(filePath));
HttpContext.Current.Response.End();

Firefox have error at file viewer.js, message undefined, line 1453.

Response in Network in debugger of Firefox is 200 OK.

But message in browser is connection lost (interupted).

Upvotes: 2

Views: 6431

Answers (1)

Daniel Rusnok
Daniel Rusnok

Reputation: 489

both advices help. Thank you guys. this is how looks correct answer:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", $"inline; filename=\"objednavka-{ OrderId}.pdf\"");
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
HttpContext.Current.Response.AddHeader("content-length", File.ReadAllBytes(filePath).Length.ToString());
HttpContext.Current.Response.BinaryWrite(File.ReadAllBytes(filePath));
HttpContext.Current.Response.End();

Upvotes: 4

Related Questions