Hi,

How to Open PDF document in asp.net web page without asking save/open/cacel dialogue .

I have tried but every time it prompts for saving/opening or cancel action.

Thanks in advance.

Rakesh

Dani AI

Generated

Short answer: you can strongly hint the browser to show the PDF inline, but you cannot guarantee it for every client. Whether a dialog appears still depends on the user’s browser and whether a PDF viewer is available, which is what noted.

If you control the response, stream the PDF yourself and set the right headers. This avoids the Save/Open prompt in most cases and is more reliable than Response.Redirect suggested by and .

// e.g., Pdf.aspx.cs
using System;
using System.IO;
using System.Web;

protected void Page_Load(object sender, EventArgs e)
{
    var path = Server.MapPath("~/files/report.pdf");  // your file path
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "inline; filename=report.pdf");
    Response.AddHeader("Content-Length", new FileInfo(path).Length.ToString());
    Response.TransmitFile(path);     // or Response.WriteFile(path)
    Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest(); // avoid ThreadAbort from Response.End()
}

Why this helps:

  • Content-Type: application/pdf tells the browser the media type.
  • Content-Disposition: inline asks the browser to render in-page (not as a forced download). This is defined by the HTTP spec and widely honored, though user settings can override it. See RFC 6266 for details.

Practical tips:

  • If the PDF is served as a static file and you cannot change code, add a custom response header at the site/app level in IIS: Content-Disposition: inline. This can be done in IIS Manager under HTTP Response Headers. Reference: IIS custom headers.
  • The code-behind approach above is straight from Microsoft’s guidance on writing binary files to the browser. See: Write binary files to the browser (ASP.NET, C#).

You can also embed the URL in an <iframe> or <object> for a nicer UX, but the same headers and client capability still govern behavior.

Recommended Answers

All 8 Replies

Hi,

Thanks for your prompt reply.

I had tried the same things also but it invokes the dialogue box for saving/opening.

Regards,
Rakesh

hmmmm are you sure that gives a dialog pop up? Try this one:

try a response.redirect to the document. i should not ask.

Hey, maybe

Response.Redirect("pdfpath/pdfdoc.pdf");

is what you're looking for?

EXCELLENT SINGLE LINE ANSWER! laurichi

I'm not sure that is the right solution, because if you redirect to a pdf file so the user on the other side has to have AcrobatReader on his machine, AcrobatReader know to open the file on the explorer, think of it as en addin to explorer so with out it the explorer does not know how to open the PDF file.
There is a simple solution posted by Microsoft in the next link:
http://support.microsoft.com/kb/307603/EN-US/
I think its a better solution, but what ever you like.

There are various approaches used to display PDF documents in a webpage. Displaying PDF documents in a webpage using simple ASP.NET custom server control or Directly rendering the PDF documents on the webpage. So if you want to know more details on these approaches, you can refer following links.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.