Click here to Skip to main content
15,888,271 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried this question earlier but did not get any real answers, so perhaps I am not explaining it correctly. Let me try it this way. My program searches for a (PDF) document that is stored in a web folder online. It is then supposed to display it in an iFrame on a web-page. I have tried this two ways, both as an HTML5 web-page iFrame and also as a google docs. Code for Google Docs is shown below, but both produce the same result which is a blank page (no errors) but BOTH work perfectly run run as a localHost page from Visual Studio 10:

C#
//Page 1
//Create a new SQL Connection Object
        SqlConnection Conn = new SqlConnection(CONNSTR);

        try
        {
            string Sql = "SELECT [NAME],[LOCATION] FROM TTW_MISC WHERE [NAME] = @NAME ORDER BY [NAME] ASC ";

            //Set Command Object Headers
            SqlCommand Comm = new SqlCommand(Sql, Conn);
            //For an Sql Command
            Comm.CommandType = CommandType.Text;
            //For a Stored Procedure
            //Comm.CommandType = CommandType.StoredProcedure;

            //Comm Paramemters
            Comm.Parameters.Clear();
            Comm.Parameters.AddWithValue("@NAME", "TTW_Story.pdf");

            //Create a Dataset
            SqlDataAdapter da = new SqlDataAdapter(Comm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            location3 = ds.Tables[0].Rows[0]["LOCATION"].ToString();
            CEO1 = ds.Tables[0].Rows[0]["NAME"].ToString();
        }
        catch (Exception ex)  //Catch Errors
        {
            Console.WriteLine("Error : " + ex.Message);
        }

        Session["Document"] = location3 + CEO1;
        Response.Redirect("TTW_Doc_Reader.aspx", false);

//Page 2 (iFrame)
        //Using iFrame with Google Docs
        if (Session["Document"].ToString() != string.Empty)
        {
            string document = Session["Document"].ToString();
            string pdfPage = "http://docs.google.com/gview?url=" + document + "&embedded=true";
            pdiFrame.Attributes.Add("src", pdfPage);
        }
        else
        {
            ClientScriptManager script = Page.ClientScript;
            if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
            {
                script.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('Sorry, this document cannot be found. ')", true);
                return;
            }
        }


Since it works perfectly when it is run in VS 10, I "ASSUME" that the it is being passed correctly online since it is the same page, and a check in the loaded page source appears to verify that, but all I get is a blank page. All I can think of is some kind of security issue that is not letting the page come down from the server, but I do not know how to check this, or even if I am on the right track. Why does it work from the desktop? I'm more than a week on this now. I have received great assistance from Code Project over the years and have always resolved the problem. I thank you in advance for your assistance! PLEASE PLEASE HELP. Thanks again, Pat.

What I have tried:

I have tried both Google docs and HTML 5 iFrame direct code. I have verified the source document and it's existence. I have checked the code for accuracy and it resolves perfectly when run in Visual Studio 10. I have checked the web page source to make sure it is looking for a real page and that the page format code is correct (which it must be since it is found and displayed perfectly in both formats when run from the localhost). I have tried 3 different browsers (Firefox, Chrome, Explorer). I have decimated the internet with searches for any similar issue and I have found nothing that matches my issue, considering mine already works when done from the VS locahost.
Posted
Updated 26-Mar-17 18:32pm
Comments
Richard Deeming 28-Mar-17 12:24pm    
Is your live site using an SSL certificate? If so, you won't be able to embed an iframe pointing to an HTTP source.

Try using the secure version of the Google Docs site instead:
string pdfPage = "https://docs.google.com/gview?url=" ...
PDTUM 28-Mar-17 13:26pm    
Wow Richard. Great thought. The answer is YES, it is a secure site. I told my network architect that I thought it was a config issue, and now I might be vindicated. I don't know if this is the fix, but it is sure promising. I REALLY APPRECIATE the idea and I am going to try it out shortly. I spent the entire day yesterday undoing all the old code and changing to JavaScript (which I hate but it does work). I would happily invest another day to put it all back. Just one more question if I may? Why is there an issue with the iFrame? Also, I got it to work correctly in IE Edge on Windows 10 using an Embed code (posted below), but it would not work in Chrome or Firefox (which is my developer default). I'll post results here. Keep in touch. Code follows. Best Regards, Pat.

(This works in Edge!)
pdfViewer.InnerHtml = "<embed src='" + item + "' type='application/pdf' scrolling='auto' height='530px' width='560px'>";
Richard Deeming 28-Mar-17 13:52pm    
When you load a page over HTTPS, that means you don't want anyone to be able to intercept or tamper with it.

If your page then loaded content over HTTP, it would be possible for a MitM (man-in-the-middle) to modify that content, and compromise the security of your site.

As a result, most browsers will block pages served over HTTPS from loading "active" content (scripts, css, fonts, iframes, etc.) from HTTP sources. Depending on the settings, some will also block "inactive" content like images.

The reverse is not an issue - there's no problem with pages served over HTTP loading resources from an HTTPS site. So the simplest solution is to always load external resources over HTTPS, which will always work.

(The other option is "protocol-relative" URLs, where you omit the "http:" or "https:", and just start with "//"; but they're no longer recommended.)
PDTUM 28-Mar-17 13:46pm    
And so ... I am sorry to say it did not make a difference. Just in case, I am posting what I tried below. I also tried hard coding the string into the src call on the aspx page. I think it was a GREAT idea, and I thought this was it. Back to square one. If you have any other thoughts or see a problem with my code, let me know. Many thanks, Pat.

string pdfPage = "https://docs.google.com/gview?url=" + doc + "&embedded=true";
pdf.Attributes.Add("src", pdfPage);
Richard Deeming 28-Mar-17 13:55pm    
Try loading the document URL directly in Fiddler[^], and look at the response headers. Is there an X-Frame-Options[^] header?

1 solution

The iFrames are not reliable and Google doesn't allow it's pages to be rendered in them as far as I know.

My suggestion would be to use some kind of other control or modal. Get the data through jQuery Ajax or something.
 
Share this answer
 
Comments
PDTUM 27-Mar-17 12:15pm    
Hello Tandit, THANK YOU for your reply!

So, the reason that I used Google Docs at all is that I could not get it to work in an HTML5 iFrame and some others I have spoken to have used this technique successfully with Google Docs. Below is the original code:

if (Session["Document"].ToString() != string.Empty)
{
string document = Session["Document"].ToString();
pdfIframe.Attributes.Add("src", document);
}

Although you may be right about Google Docs (I do not know), that does not explain why it will not work with a regular iFrame, as it is supposed to in HTML5. This is not a contest or a challenge. I greatly appreciate your reply and suggestion. With that said, I find it incredible to believe that there is no real C# solution to address viewing a PDF in an HTML format! Yes, I could easily get the job done by using a text area with a database query (Fast and Easy!), but it looks so unprofessional. I even considered taking a picture of the document and posting it as a JPG, however, then multiple pages and scrolling create a new problem. This is very discouraging. I personally really dislike using JAVA and I avoid it except where absolutely necessary. This is why I choose to write desktop applications and I avoid the internet unless it is absolutely necessary. If you could explain why the iFrame will not work or if you know of any other solution, please let me know. I will be at my desk working for at least the next 6 hours. THANK YOU AGAIN for your input and advice. It is appreciated! Pat.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900