Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with IFrame refresh 1

Status
Not open for further replies.

MdotButler

Programmer
Jan 18, 2006
104
US
I have a page that contains an IFrame which I use to display PDF reports that are stored in a database. The process is to obtain the PDF from the database, write it to a file and then set the IFrame source property to display it.

The problem is that it works on the initial image but will not refresh when the src property is set to another file. I created a test form using the nwind database and JPG images. BTW the process works in debug mode from within VS 2008 but fails when uploaded to the IIS server.

aspx code
Code:
<%@ Page Language=&quot;VB&quot; AutoEventWireup=&quot;false&quot; CodeFile=&quot;IFrame_1.aspx.vb&quot; Inherits=&quot;IFrame_1&quot; Title=&quot;Test IFrame Refresh&quot; %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Test IFrame Refresh</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="1.JPG" />&nbsp;
        <asp:Button ID="Button6" runat="server" OnClick="Button6_Click" Text="2.JPG" />&nbsp;
        Using OnClick, writing DB image to disk to display
        <br /><br />
        <iframe id="ifrReport" runat="server" width="700px" height="800px" style="z-index: -1" />
        </div>
    </form>
</body>
</html>

code behind
Code:
Imports System.IO
Imports System.Data
Imports System.Data.OleDb

Partial Class IFrame_1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Response.CacheControl = "no-cache"
        'Response.AddHeader("Pragma", "no-cache")
        'Response.Expires = -1

    End Sub
    '======================================================
    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        DisplayImage(5)
    End Sub
    '======================================================
    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        DisplayImage(6)
    End Sub
    '======================================================
    Protected Sub DisplayImage(ByVal intRecno As Integer)
        Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("~/App_Data/nwind.mdb"))

        Dim strSqlCommand As String = "SELECT Photo FROM Employees WHERE EmployeeID = " + CStr(intRecno)
        Dim myAdapter As New OleDbDataAdapter(strSqlCommand, myConnection)
        Dim myDataSet As New DataSet
        myAdapter.Fill(myDataSet, "Employee")
        Dim myDataRow As DataRow = myDataSet.Tables("Employee").Rows(0)

        ' build file and set iframe source
        Dim strFileName As String = MapPath("~/Uploads/") + "123.jpg"
        If File.Exists(strFileName) Then
            File.Delete(strFileName)
        End If

        Dim arrFile As Byte() = myDataRow("Photo")
        Dim binWriter As New BinaryWriter(File.Open(strFileName, FileMode.Create))
        binWriter.Write(arrFile)
        'binWriter.Flush() ' made no difference
        binWriter.Close()
        'ifrReport.Attributes.Remove("src") ' made no difference
        ifrReport.Attributes.Add("src", "Uploads/123.jpg")
        ifrReport.Visible = True
    End Sub
End Class
TIA
Mark
 
Brilliant... I changed the line setting the iframe source to the following and it worked as advertised.

I suspected that it was a cache issue and tried a few things in the page_load code to prevent the page from being cached prior to posting without success.
Code:
ifrReport.Attributes.Add("src", "Uploads/123.jpg?" + CStr(intRecno))
I did discover after posting the original message that doing a refresh in the browser then displayed the correct picture.

Thanx Mark for the help...
 
Just a quick point to make sure that if you are using one file that you are actually displaying the correct picture (i.e. if two requests come in simultaneously, that the first request isn't seeing the file requested by the second request)!

You may be better off storing each photo using the employee's unique number and having a clean up routine that deletes files after a certain amount of time.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top