CSharpBeginner
CSharpBeginner

Reputation: 611

Load local html on iframe

I have in my server .html page located on c:\test\test.html.

I want to display test.html inside iframe on the client machine, how can I do that?

What I tried:

<iframe id="serviceFrameSend" src="file:///c:\test\test.html" width="1000" height="1000"  frameborder="0">

But it's found the test.html file on the client machine, so how can I make that the test.html will be loaded from the server?

If it isn't possible, how can I do that in another way?

Upvotes: 20

Views: 126993

Answers (5)

Tej Patil
Tej Patil

Reputation: 115

Solutions (Pick one):

  1. place "test.html" file the public directory on the server.
  2. Or, you can also make "test" directory publicly accessible.

Upvotes: 1

user19145538
user19145538

Reputation: 1

html iframe element cannot load an empty webpage (no text or images on the body element).

I mistakenly used an empty webpage, it was not loading. the comment above help me to fix it.

Upvotes: 0

DomeTune
DomeTune

Reputation: 1421

As you have the page on your server you need to use an http:// url, not file:///. It's a little tricky because the web server has a different syntax for file paths than your file system. Here are some options.


<iframe id="serviceFrameSend" src="test.html" width="1000" height="1000"  frameborder="0">
<iframe id="serviceFrameSend" src="./test.html" width="1000" height="1000"  frameborder="0">

You can use these if test.html is in the same directory as your main-page. (The main-page is the html with the iframe in it.)


<iframe id="serviceFrameSend" src="../test.html" width="1000" height="1000"  frameborder="0">

You can use this if test.html is in the parent directory of your main-page, as long as it doesn't go beyond the web server's "root" directory.


<iframe id="serviceFrameSend" src="../test/test.html" width="1000" height="1000"  frameborder="0">

You can use this if the test.html is in the sibling directory of your main-page, such as path/views/test/test.html when your main-page is path/views/main/page.html.


<iframe id="serviceFrameSend" src="https://www.server.com/test/test.html" width="1000" height="1000"  frameborder="0">

Or, you can always use an absolute path, based on the complete url of your test.html.

Upvotes: 20

Shadow Wizard
Shadow Wizard

Reputation: 66389

You will have to use server side language like PHP, ASP.NET, node.js etc and create a "proxy", that will get the desired file as parameter, read the file on the server, and send its contents.

For example, in ASP.NET you can have such code:

Download.aspx

<script language="C#" runat="server">
void Page_Load(object sender, EventArgs e)
{
    int id;
    if (!Int32.TryParse(Request.QueryString["id"], out id))
    {
        Label1.Text = "Missing or invalid ID";
        return;
    }

    string filePath = "";
    switch (id) {
        case 1:
            filePath = "c:\\test\\test.html";
            break;
    }

    if (filePath.Length == 0)
    {
        Label1.Text = "ID " + id + " does not exist";
        return;
    }

    if (!System.IO.File.Exists(filePath))
    {
        Label1.Text = "Requested file '" + filePath + "' does not exist";
        return;
    }

    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
    Response.Clear();
    Response.WriteFile(fileInfo.FullName);
    Response.Flush();
    Response.End();
}
</script>
<!DOCTYPE html>
<html>
<body>
<form id="form1" runat="server">
    <asp:Label ID="Label1" runat="server"></asp:Label>
</form>
</body>
</html>

Then have such iframe:

<iframe id="serviceFrameSend" src="Download.aspx?id=1" width="1000" height="1000" frameborder="0"></iframe>

Upvotes: 1

Tihi
Tihi

Reputation: 1

You can't load from your PC like c:... You only can load file, from your server. If this html file, and test.html file is in a same directory on your server, easily load with test.html, if it is in another directory, then use the directory name like, test/test.html

Upvotes: 0

Related Questions