Domanda

Impostare l'attributo src di un IFrame su data: application / pdf; base64, non funziona per me, qualche idea sul perché?

Ecco il markup .aspx

<%@ 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>

ed ecco il codice dietro:

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();

    }

}

Devo dire che, diversamente da questo esempio, nell'app reale, i dati pdf vengono generati sul lato server. Il motivo per cui sto provando a caricare i client di dati pdf sul lato client è che devo avere i client di dati pdf byte in ogni caso per fare qualcos'altro e sto cercando di ridurre le istanze di questi dati generati e ridimensionati.

Basta inserire il codice sopra riportato e eseguire il markup in un semplice sito Web di una pagina in VS2005 e incollare qualsiasi vecchio pdf in c: \ temp \, chiamarlo TestDoc.pdf e dovrebbe essere compilato ed eseguito.

Fondamentalmente il comportamento che sto ottenendo non è affatto nell'iframe.

Sto usando IE7, quindi potrebbe essere un problema. Non lo so dato che ci sono preziose informazioni sull'uso dei dati: application / pdf; base64 [sintassi dati base64] in giro.

È stato utile?

Soluzione

Per quanto ne so, IE non gestisce affatto i dati: schema URL, quindi suppongo che non sappia cosa passare al visualizzatore PDF. Vedi WP .

Saluti,

Altri suggerimenti

Potrebbe essere pessimo fare riferimento a Wikipedia, ma Wikipdia dice che potrebbero esserci alcune restrizioni quali tipi di file ti è permesso usare i dati: filetype; sintassi base64 su. Vale a dire, solo le foto per ora. Le specifiche IE9, dice l'articolo, consentono un uso più ampio, ma non sono sicuro di cosa significhi esattamente. Nel frattempo, dovrai solo usare il file .pdf e non solo i suoi dati di stringa di base 64.

in questo modo ha funzionato per me:

var oldsrc = $('.content-control-iframe').attr('src');
var newsrc = $('.content-control-iframe').attr('src').slice(8);
$('.content-control-iframe[src="'+oldsrc+'"]').attr('src', newsrc);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top