【问题标题】:ASP.NET iframe doesn't update, sometimesASP.NET iframe 有时不更新
【发布时间】:2015-07-07 13:43:59
【问题描述】:

下面是我简化的 .aspx 文件。有时,单击“更新预览”按钮后,包含已修改 .pdf 文件的 iframe 会显示最新文件。不过,有时需要多次点击,iframe 才会显示最新文件。

即使我直接在新窗口中重定向到文件,.pdf 文件仍然不是最新的! (不过,在我的服务器磁盘上)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="pdfwebform.WebForm1" %>

<script runat="server">
void download_Click(object sender, EventArgs e)
{
    Response.Redirect("http://dev-db-02/pdf/catalog.pdf");
}

void Button1_Click(object sender, EventArgs e)
{
    string answer = call_pdf(make_new, retails_array, restypes_array, selected_kids, brands_array);
    if (answer != "failed")
    {
        label1.Text = "Done!";
    }
    else
    {
        label1.Text = "No models meet selected criteria.";
    }
}

</script>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Generate PDF Catalog</title>
        <link href="theme.css" rel="stylesheet" type="text/css" />
        <!--
        <style type="text/css">
            .auto-style1 {
                width: 100%;
                height: 105px;
            }
            .auto-style2 {
                width: 557px;
            }
        </style>
        -->
        <style type="text/css">
            .auto-style1 {
                width: 84%;
            }
            .auto-style2 {
                text-align: left;
                width: 87px;
            }
            .auto-style3 {
                text-align: left;
                width: 486px;
            }
        </style>
    </head>
    <body>
        <script type="text/javascript">
        function appear() {
            document.getElementById("preview").style.display = 'block';
        }
        </script>

        <script type="text/javascript">
        function updatepreview() {
            if (document.getElementById("label1").innerHTML != "") {
                var iframe = document.getElementById("preview1");
                iframe.src = iframe.src;
                iframe.src = iframe.src;
                iframe.src = iframe.src;
            }
            else{
                document.getElementById("label1").innerHTML = "Please Wait...";
            }
        }
        </script>

        <script type="text/javascript">
            function update_iframe() {
                var iframe = document.getElementById("preview1");
                iframe.src = iframe.src;
                iframe.src = iframe.src;
                iframe.src = iframe.src;
            }
        </script>

        <script type="text/javascript">
        function dot() {
            if (document.getElementById("label1").innerHTML == "d") {
                document.getElementById("label1").innerHTML = "nope";
            }
            else {
                document.getElementById("label1").innerHTML = "d";
            }
        }
        </script>

        <h1>Generate PDF Catalog</h1>
        <form id="form1" runat="server">
            <!-- <embed id="preview" src="http://dev-db-02/pdf/catalog.pdf" width="500px" height="500px" runat="server" /> -->
            <iframe id="preview1" src="http://dev-db-02/pdf/catalog.pdf" width="500" height="500" runat="server"></iframe>
                <!-- Add to File Feature
                <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                    <asp:ListItem Selected="True" Value="new">Create New PDF</asp:ListItem>
                    <asp:ListItem Value="old">Add to Existing PDF</asp:ListItem>
                </asp:RadioButtonList>
                -->
                <table class="auto-style1">
                    <tr>
                        <td class="auto-style2">
                            <b>Retail:<br />
                            </b>
                <asp:ListBox ID="ListBox5" runat="server" Height="45px" SelectionMode="multiple" Width="230px">
                    <asp:ListItem Selected="True" Value="Yes">Yes</asp:ListItem>
                    <asp:ListItem Value="No">No</asp:ListItem>
                </asp:ListBox>
                <b>
                            <br />
                            <br />
                            Res Type:</b><br />
                <asp:ListBox ID="ListBox4" runat="server" SelectionMode="Multiple" Height="69px" Width="230px">
                    <asp:ListItem Selected="True" Value="OPTICAL">OPTICAL</asp:ListItem>
                    <asp:ListItem Value="KIDS">KIDS</asp:ListItem>
                    <asp:ListItem Value="SUN">SUN</asp:ListItem>
                </asp:ListBox>
                            <br />
                            <br />
                            <b>Brand:</b><br />
                <asp:ListBox ID="ListBox3" runat="server" Height="142px" Width="230px" SelectionMode="multiple" >
                    <asp:ListItem Selected="True" Value="'AS'">ASPIRE</asp:ListItem>
                    <asp:ListItem Value="'BCBG'">BCBGMAXAZRIA</asp:ListItem>
                    <asp:ListItem Value="'CVO'">CLEARVISION</asp:ListItem>
                    <asp:ListItem Value="'CH'">COLE HAAN</asp:ListItem>
                    <asp:ListItem Value="'DD'">DILLI DALLI</asp:ListItem>
                    <asp:ListItem Value="'DH'">DURAHINGE</asp:ListItem>
                    <asp:ListItem Value="'ET'">ELLEN TRACY</asp:ListItem>
                    <asp:ListItem Value="'IZOD'">IZOD</asp:ListItem>
                    <asp:ListItem Value="'IZX'">IZOD PERFORMX</asp:ListItem>
                    <asp:ListItem Value="'JMC'">JESSICA MCCLINTOCK</asp:ListItem>
                    <asp:ListItem Value="'JC'">JUNCTION CITY</asp:ListItem>
                    <asp:ListItem Value="'ME'">MARC ECKO</asp:ListItem>
                    <asp:ListItem Value="'OP'">OCEAN PACIFIC</asp:ListItem>
                    <asp:ListItem Value="'PT'">PURITI</asp:ListItem>
                </asp:ListBox>
                            <br />
                            <br />
                            <br />



                        </td>
                        <td colspan="3">
            <asp:Label id="label1" runat="server" Font-Bold="true" onchange="updatepreview()"></asp:Label>
                            <br />
            <!-- <embed id="preview" src="http://dev-db-02/pdf/catalog.pdf" width="500px" height="500px" runat="server" /> -->
            <iframe id="preview1" src="http://dev-db-02/pdf/catalog.pdf" width="500" height="500" runat="server"></iframe>
                            <br />

            <!-- <iframe id="frame" style="display:none"></iframe> -->
            <!-- <a href="javascript:download('http://dev-db-02/pdf/catalog.pdf')">download</a> -->

                    &nbsp;</td>
                    </tr>
                    <tr>
                        <td class="auto-style2">

                <asp:Button id="Button1" runat="server" Font-Bold="True"
                Font-Names="Verdana" Font-Size="12" OnClick="Button1_Click" OnClientClick="updatepreview()"
                Text="Submit"></asp:Button>


                        </td>
                        <td class="auto-style3">

                            *<font size="3">Hold Control/Command 
                            <br />
&nbsp;+ Click to select multiple</font>

                        </td>
                        <td class="auto-style3">
                            <font size="3">
                <asp:Button ID="Button2" runat="server" Text="Update Preview" OnClientClick="update_iframe()" />

                            </font>

                        </td>
                        <td class="align-right">
                            &nbsp;</td>
                    </tr>
                </table>

                <br />
                <br />
            </div>
        </form>
        <script type="text/javascript">
            function download() {
                window.open("http://dev-db-02/pdf/catalog.pdf");
            }
        </script>
    </body>
</html>

【问题讨论】:

  • 请不要把解决的放在标题里。当答案获得支持或您已接受答案时,该问题被视为“已解决”。

标签: javascript c# jquery asp.net iframe


【解决方案1】:
 void download_Click(object sender, EventArgs e)
{
    Response.ContentType = "Application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=help.pdf");
    Response.TransmitFile(Server.MapPath("~/pdf/catalog.pdf"));
    Response.End();
}

在您的情况下,您将使用 301 的响应重定向到 url 女巫返回您的文件,问题是您正在使用 301 重定向,即使您正在更改磁盘文件浏览器也会返回缓存的 301 重定向(同一文件)

【讨论】:

  • 这让我可以真正下载文件,这是我真正想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多