Santosh Kumar
Santosh Kumar

Reputation: 69

How to Open pdf in the browser

i have a gridview within which there are link button on clicking link button i able to download file like,pdf,jpeg,doc but i want display the browser itself on clicking link button

string pdfPath = Server.MapPath("~/SomePDFFile.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(pdfPath);
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);

but i want open file like jpeg,pdf,doc gridview inside a link button in the browser

Upvotes: 0

Views: 6189

Answers (4)

SamuraiJack
SamuraiJack

Reputation: 5539

You can display the pdf, jpg etc in an iframe on the same page or another. For eg:

<iframe id="iframe" runat="server" src="NameOfYourFolder/pdf.pdf"></iframe>

Here i have hardcoded the path to the pdf file which will be displayed within the iframe. You need to create a mechanism to get the path of the file associated with that linkbutton in the GridView and pass that path to iframe's src through embedded code blocks.

Upvotes: 0

Niventh
Niventh

Reputation: 995

First Way to show PDF in browser

protected void btnOpen_Click(object sender, EventArgs e)
{
Response.Redirect("SiteAnalytics.pdf");
}

Second way to Show PDF in browser by setting Content Type of the Response object and add the binary form of the pdf in the header

protected void btnpdf_Click(object sender, EventArgs e)
{
string path = Server.MapPath("SiteAnalytics.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);

if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}

Upvotes: 1

Knaģis
Knaģis

Reputation: 21475

You should specify Content-Disposition: inline header. If the browser will support the display of the type you specified in Content-Type header, it will be displayed in the browser.

The opposite is Content-Disposition: attachment which will force the browser to provide Save As button for your content (even when it is HTML).

Upvotes: 0

Grant Thomas
Grant Thomas

Reputation: 45083

This depends on the file type and how the current system is configured to handle that type.

For instance, most images will just open in a browser by default; however, for me, I have Chrome configured as the default registered application for PDF files, yet this is hardly a standard thing right now, many many general users will have just what they were told to download when installing something else, something awful like Foxit reader - and in such cases, where the browser isn't registered to handle the file type, it won't happen.

Upvotes: 1

Related Questions