0

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"

3
  • I would suspect this is an absolute vs relative link issue. If you are trying to view this in a browser, files linked from the hard disk are blocked by default for security. How you are testing this - remotely, or locally? Commented Jun 14, 2013 at 21:39
  • I'm testing this locally, using the asp.net development server that starts when you push the "play" button in Visual Studio Commented Jun 14, 2013 at 21:43
  • Can you try putting the PDF file in whatever folder VS uses when executing this and change the link so it doesn't include the C:/? It might be you application's folder, but not sure. Can you try uploading the file and then using it's http:// link to test if it works? If it does, you know it is a problem with your path. Commented Jun 14, 2013 at 23:03

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Now I get a 404 from Requested URL: /~/Data/Untitled1.pdf. Except that I don't actually type that first / in there, it just appears
Where is your data folder is located? Is it accessible via HTTP url?. Apparently it is a path issue and IFRAME cannot load a file:// source
my Data is at WebApplication1\WebApplication1\Data Relative to the code, it's in the same directory. The code is also in WebApplication1\WebApplication1
I changed ~ to a . and it worked. Just a hunch based on how certain shells navigate paths. Hopefully when I put this on a server it uses the same standards...
2

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.

3 Comments

That gives me the same results. But I have new information about the file path. I'll edit my question to include it. Yes, the path is available from my browser, but I have to replace localhost with file. Is that what you mean?
What happens if instead of filePath as a test you would assign a literal path that you can see in browser?
The same thing. I assigned file:///C:/Users/Houseman/Desktop/WebApplication1/WebApplication1/Data/Untitled1.pdf and it came up blank. If I put that same path into a browser, it'd take me right to it.