[英]display aspx in iframe and aspx is displaying pdf
我使用iframe顯示aspx頁面如下
<a href="#" id="trigger">this link</a>
<div id="dialog" style="display:none">
<div>
<iframe frameborder="0" height="600" width="600" src="Displaypdf.aspx"></iframe>
</div>
</div>
在Displaypdf.aspx.cs中,我正在顯示如下pdf。 我在aspx中有一個按鈕,單擊該按鈕會顯示pdf(在aspx頁面中,現在是iframe)
protected void Button1_Click(object sender, EventArgs e)
{
string FilePath = Server.MapPath("sample.pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
這很好。 現在我想在Page_Load中做同樣的事情,但是當我將此代碼放入頁面加載中時,這不起作用。
protected void Page_LOad(object sender, EventArgs e)
{
string FilePath = Server.MapPath("sample.pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
發生的是,我單擊並獲取了我的iframe,但是iframe是空白的,因為第一次沒有顯示mein pdf。 有人可以告訴我如何解決此問題。
我已經嘗試了您的代碼,但我認為您缺少一些使其起作用的東西。 我在下面提供了您的代碼的編輯版本,該版本應該可以正常運行。
iframe頁面(aspx):
<head>
<script src="../../Scripts/jquery-1.9.1.min.js"></script>
<script src="../../Scripts/jquery-ui.js"></script>
<script type="text/javascript">
var $dial1 = ""
function openlink(url, title, width, height) {
$dial1 = $('<div></div>')
.html('<iframe id="frame1" style="border: 0px; " src="' + url + '" width="100%" height="100%"></iframe>')
.dialog({
autoOpen: false,
modal: true,
height: height,
width: width,
title: title
});
$dial1.dialog('open');
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="This Link" OnClientClick="openlink('Displaypdf.aspx', 'Open', '1000', '470'); return true;" OnClick="Button1_Click" />
</form>
</body>
Displaypdf.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string FilePath = Server.MapPath("sample.pdf");
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.