4 ways to send a PDF file to the IE Client in ASP.NET 2.0

本文介绍了一种使用C#网站项目直接在Internet Explorer客户端中发送PDF文件的方法,分为四个步骤:1) 刷新页面直接显示PDF;2) 提示用户保存PDF并预设文件名;3) 类似于步骤2,但文件名为页面名称;4) 在新窗口中打开PDF,无需提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this pick, I am sharing a project using which you can send PDF files to the IE Client in four different ways. You may download the project by clicking here.

Create a new C# Website. While creating the following pages ensure that you DON'T have "Place code in Seperate file" checkbox as checked. You need to create 4 pages called "Way1.aspx", "Way2.aspx", "Way3.aspx" and "PDFContainer.aspx" without a code behind.

So the four different ways which I was talking about is as follows...

1) You hit a button on a page and the page refreshes itself with a PDF file in the same instance of the IE Browser. You are not prompted to Open/Save/Cancel by the IE Client at all. The code for this page is as follows

Way1.aspx

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.Clear();
        Response.TransmitFile("UML.pdf");
        Response.End();
    }
</script>
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Way 1</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send PDF" /><br />
        <br />
        This page will refresh itself with a PDF file opened in the same instance of IE itself.
        Users will not get prompted to Save/Open/Cancel the PDF file.</div>
    </form>
</body>
</html>

2) You hit a button on a page and the page throws a Dialogbox saying Open/Save/Cancel. You click on Save and the FileName is populated is the document is according to your taste (meaning you have to supply it explicitly).

3) Verify similar to 2 above, but the filename in the Dialogbox is the name of the Page (meaning, if you don't the supply name, the Dialogbox takes the name of the aspx file as the PDF file name).

The code for this page is as follows...

Way2.aspx

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment;Filename=UML.pdf");
        Response.TransmitFile("UML.pdf");
        Response.End();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment");
        Response.TransmitFile("UML.pdf");
        Response.End();
    }
    </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Way 2</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send PDF" /><br /><br />
        This button will do a postback and the Client will be prompted with a Dialog Box to Save/Open/Cancel the PDF file transmission. The thing to be noticed here is that the FileName in the Dialogbox will be automatically set as UML.pdf <br /><br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Send PDF without FileName" /><br /><br />
        This button will do a postback and the Client will be prompted with a Dialog Box to Save/Open/Cancel the PDF file transmission. The thing to be noticed here is that the FileName in the Dialogbox (Way2.pdf) is set as the Page Name, which is Way2.aspx</div>
    </form>
</body>
</html>

4) You hit a button on a page and the page opens a new instance of Internet Explorer and loads the PDF file in the IE Client itself without prompting to Open/Save/Cancel. The code for these pages Way3.aspx & PDFContainer.aspx are as follows...

Way3.aspx

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script  language=vbscript>
sub OpenInNewWindow()
    window.open("PDFContainer.aspx")
end sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Way 3</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type=button OnClick="OpenInNewWindow()"  value="Open in New Window"/>
        <br /><br />
        This time, we will open a new Internet Explorer Window and Show the PDF File directly in the IE
    </div>
    </form>
</body>
</html>

PDFContainer.aspx

 

UPDATE:By the way, I just found out... these ways will not work with Adobe Reader 6.0 or below. We upgraded to Adobe Reader 7.0 and everything works as expected.

Hope that helps!

-Rahul Soni

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=undefined&dt=1192552645359&lmt=1192552645&format=undefinedxundefined&output=html&correlator=1192552645359&url=https%3A%2F%2Fblue-sea-697d.quartiers047.workers.dev%3A443%2Fhttp%2Fblog.csdn.net%2FTeng_s2000%2Farchive%2F2006%2F04%2F25%2F676440.aspx&ref=https%3A%2F%2Fblue-sea-697d.quartiers047.workers.dev%3A443%2Fhttp%2Fwww.baidu.com%2Fs%3Flm%3D0%26si%3D%26rn%3D10%26ie%3Dgb2312%26ct%3D0%26wd%3Dasp%252Enet%2Bie%2Bpdf%26pn%3D20%26cl%3D3&cc=19&ga_vid=178692576.1192193852&ga_sid=1192552646&ga_hid=791027653&ga_fc=true&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" scrolling="no" allowtransparency="allowtransparency">
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.Clear();
        Response.TransmitFile("C://umlclass.pdf");
        Response.End();       
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>PDF File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
    </div>
    </form>
</body>
</html>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值