将iframe的数据src属性:应用程序/ PDF格式; BASE64? [英] Setting the src attribute of an IFrame to data:application/pdf;base64?

查看:1571
本文介绍了将iframe的数据src属性:应用程序/ PDF格式; BASE64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置一个IFrame数据的src属性:应用程序/ PDF格式; BASE64,不工作对我来说,任何想法,为什么

Setting the src attribute of an IFrame to data:application/pdf;base64, isn't working for me, any ideas why?

下面是在.aspx标记

Here's the .aspx markup

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function loadIFrameFromHiddenField()
        {         
            //get the node containing the base64 pdf data from the xml in the hidden field
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.loadXML(document.getElementById("pdfData").value);                    
            xmlDoc.setProperty('SelectionLanguage', 'XPath');
            var pdfDataNode = xmlDoc.selectSingleNode("//PDF");

            //if we've got the node
            if (pdfDataNode != null) 
            {
                //get the data and append it to the src contents 
                var pdfIFrameSrc = "data:application/pdf;base64," + pdfDataNode.text;
                //set the src attribute
                document.getElementById("pdfIFrame").setAttribute("src", pdfIFrameSrc);
            }            
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" style="width:100%;height:100%;">
        <asp:HiddenField ID="pdfData" runat="server" /> 
        <div style="width:100%;height:80%;"> 
            <iframe id="pdfIFrame" runat="server" scrolling="auto" frameborder="0" marginheight="0" marginwidth="0" style="height:99.5%;width:99.5%" />            
        </div>
    </form>
</body>
</html>

和这里的背后code:

and here's the code behind:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Xml;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //get the bytes from our PDF
        Byte[] pdfBytes = File.ReadAllBytes("c:\\temp\\Test.pdf");

        //build xml containiing our base64 encoded pdf data and put it in hidden field
        pdfData.Value = buildDocumentXML(pdfBytes, "TestDoc");

        //call js function to add the src to the iframe
        String scriptText = "<script type='text/javascript'>loadIFrameFromHiddenField()</script>";
        ClientScript.RegisterStartupScript(this.GetType(), "loadIFrameFromHiddenField", scriptText);
    }

    private string buildDocumentXML(Byte[] pdfBytes, string documentName) 
    {

        StringBuilder documentsString = new StringBuilder();
        XmlWriterSettings documentsXmlSettings = new XmlWriterSettings();

        documentsXmlSettings.Indent = false;            
        documentsXmlSettings.OmitXmlDeclaration = true;
        documentsXmlSettings.ConformanceLevel = ConformanceLevel.Fragment;
        documentsXmlSettings.NewLineHandling = NewLineHandling.None;

        using (XmlWriter documentsXmlWriter = XmlWriter.Create(documentsString, documentsXmlSettings))
        {

            documentsXmlWriter.WriteStartElement("DOCUMENTS");

            documentsXmlWriter.WriteStartElement("FILENAME");
            documentsXmlWriter.WriteString(documentName);
            documentsXmlWriter.WriteEndElement();

            documentsXmlWriter.WriteStartElement("PDF");


            documentsXmlWriter.WriteBase64(pdfBytes, 0, pdfBytes.Length);


            documentsXmlWriter.WriteEndElement();



            documentsXmlWriter.WriteEndElement();
        }



        return documentsString.ToString();

    }

}

应该说不像在本实施例,在实际应用中,PDF格式数据生成服务器端。我试图加载PDF数据客户方的原因是我必须有PDF字节的数据客户方反正做别的事情与我试图减少生成此数据的实例和打发各地。

I should say unlike in this example, in the real app, the pdf data is generated serverside. The reason I'm trying to load the pdf data clientside is I have to have the pdf byte data clientside anyway to do something else with and I'm trying to reduce instances of this data being generated and chucked around.

只要坚持上面的code和标记到VS2005一个简单的页面的网站,并坚持任何旧PDF在C:\\ TEMP \\,叫它TestDoc.pdf,它应该编译和运行

Just stick the above code and markup into a simple one page website in VS2005 and stick any old pdf in c:\temp\, call it TestDoc.pdf and it should compile and run.

基本上,我得到的行为是没有在iframe的。

Basically the behaviour I'm getting is nothing in the iframe at all.

我使用IE7因此这可能是一个问题。我不知道,因为有关于使用数据precious小资料:应用/ PDF格式; BASE64 [BASE64数据]语法各地

I'm using IE7 so that might be a problem. I don't know since there's precious little information about using the data:application/pdf;base64[base64 data] syntax around.

推荐答案

据我所知,IE不处理数据:URL方案可言,所以我想,它不知道该怎么传递到PDF阅读器。请参见 WP

As far as I know, IE doesn't handle the data: URL scheme at all, so I guess, it doesn't know what to pass to the PDF viewer. See WP.

干杯,

这篇关于将iframe的数据src属性:应用程序/ PDF格式; BASE64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆