user773737
user773737

Reputation:

Programmatically created iFrame is empty

I'm using a short bit of C# code to create an iFrame with a pdf inside of it.

However, the iFrame comes up empty.

 LiteralControl c= new  LiteralControl("<iframe id='embeddedFrame' name='embeddedFrame' runat=server src="+filePath+" width=400 height=400></iframe>");
 ph.Controls.Add(c);

Viewing the page source gives me this:

<iframe id="embeddedFrame" name="embeddedFrame" runat="server" src="C:\Users\Houseman\Desktop\WebApplication1\WebApplication1\Data\Untitled1.pdf" width="400" height="400"></iframe>

Which looks correct. I do indeed have that .pdf file in that location. There's no 404 error, it's just blank...

What am I doing wrong, or how could I fix this?


I can access the file through my browser, except that I have to take out localhost:8683 and replace it with file:///


I'm accessing the file with

string PdfLocation = System.IO.Path.Combine(Server.MapPath("Data") ,pdfn);

Where pdfn is the filename of the upload +".pdf"

Upvotes: 0

Views: 1723

Answers (2)

Jason P
Jason P

Reputation: 27012

Try this instead:

string PDFLocation = "~/Data/" + pdfn;

You're getting the absolute server file path, which isn't accessible from the browser. You need the website-relative path instead.

You'll need to combine this answer with Yuriy's answer about creating the iframe as a server control.

Upvotes: 2

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

Instead of creating literal (and btw, you cannot create runat="sever controls these way) try creating actual IFRAME control:

HtmlGenericControl c = new HtmlGenericControl();

c.TagName = "IFRAME";
c.Attributes["src"] = filePath;
c.Attributes["id"] = "embeddedFrame";
c.Attributes["name"] = "embeddedFrame";
c.Attributes["width"] = "400";
c.Attributes["height"] = "400";

ph.Controls.Add(c);

And make sure that the path is available from your browser, not only from the server.

Upvotes: 2

Related Questions