Hi guys

Need some help with an problem thats bugging me..
I have to open a .pdf file at the client side when the user clicks a button..

Opening is not a problem.. I am able to do it using the following code
which is executed when the user clicks on the button

MyFileStream = New FileStream("E:\MyWork\Test\DownloadFiles\Reports\Rpt.pdf", FileMode.Open)
FileSize = MyFileStream.Length
Dim Buffer(CType(FileSize, Integer)) As Byte
MyFileStream.Read(Buffer, 0, CType(MyFileStream.Length, Integer))
MyFileStream.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=MyReport.PDF")
Response.BinaryWrite(Buffer)


The problem is that the user first sees a 'Open/ Save / Cancel' dialog box. Only when the user clicks on 'Open' does the .pdf file get opened.
My client wants it to be opened directly when the button is clicked.
Am at a loss and any help will be appreciated...

Thanks in advance

Dani AI

Generated

Short answer: it is not possible for a server to force every client browser to skip the Open/Save dialog — that decision is ultimately made by the browser, the PDF handler/plugin and the operating system. The server can only suggest inline display; user settings or browser/plugin behavior can still present a prompt.

Practical guidance that addresses the points raised above: ensure the PDF is delivered as a normal HTTP(S) resource (not a raw local disk path), and ask the browser to display it inline by using the appropriate HTTP headers and correct MIME type. Serve the file from a URL the client can reach (window.open or location.href only work when they point to such a URL). Avoid response headers that intentionally prevent caching when aiming for inline display over HTTPS, since some older browser+PDF-plugin combinations (notably Internet Explorer + legacy Adobe Reader) will show the Save dialog if caching is disabled. Embedding the PDF in the page (iframe/object) or rendering it client-side with a library such as PDF.js gives the most consistent in-page experience across browsers.

Notes tied to the thread: is already streaming the PDF bytes from the server, which is the right general approach, but the current response headers are causing the download prompt. and are correct that client-side navigation (window.open / location.href) can open a PDF — those will only work if the path is a reachable HTTP(S) URL, not a server local file path. is also correct that the final behavior depends on the client.

Troubleshooting checklist: test the direct PDF URL in multiple browsers, inspect response headers (MIME type, content-disposition and cache headers), try an embedded viewer (iframe or PDF.js) for consistency, and accept that eliminating the dialog for every user is not possible without changing client settings.

Recommended Answers

All 4 Replies

Also is there any other way to open the file at the client side?

Hi ,
R u seaching for something like this.....

String strPop = "<script language='javascript'>" + Environment.NewLine +
"window.open('E:\MyWork\Test\DownloadFiles\Reports\Rpt.pdf"','Report','height=520,width=730,toolbars=no,scrollbars=yes,resizable=yes');" + Environment.NewLine +
"</script>" + Environment.NewLine;
Page.RegisterStartupScript("Pop", strPop);
Hope this will help u

Member Avatar for Member #119018

I think that all depends on the client's browser.

dude

i think the best thing is using javascript to open the pdf

try this:

<INPUT id="btnopen" type="button" value="open report" name="Button1" onclick="location.href='linux1-sample.pdf'">

sam

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.