简体   繁体   English

在iframe上加载本地html

[英]Load local html on iframe

I have in my server .html page located on c:\\test\\test.html . 我的服务器.html页面位于c:\\test\\test.html

I want to display test.html inside iframe on the client machine, how can I do that? 我想显示test.html内的客户机上的iframe,我该怎么办呢?

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? 但它在客户端机器上找到了test.html文件,那么如何才能使test.html从服务器加载?

If it isn't possible, how can I do that in another way? 如果不可能,我怎么能以另一种方式做到这一点?

As you have the page on your server you need to use an http:// url, not file:/// . 当您在服务器上有页面时,您需要使用http:// url,而不是file:/// It's a little tricky because the web server has a different syntax for file paths than your file system. 这有点棘手,因为Web服务器的文件路径语法与文件系统不同。 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. 如果test.html 主页位于同一目录中 ,则可以使用这些。 (The main-page is the html with the iframe in it.) (主页是带有iframe的html。)


<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. 如果test.html位于主页的父目录中,只要它不超出Web服务器的“root”目录,就可以使用它。


<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 . 如果test.html位于主页的兄弟目录中 ,则可以使用此方法,例如当您的主页为path/views/main/page.html时, path/views/test/test.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 . 或者,您可以始终使用绝对路径 ,基于test.html的完整URL。

You will have to place "test.html" file the public directory on the server. 您必须将“test.html”文件放在服务器上的公共目录中。 Or else make "test" directory publicly accessible. 或者使“test”目录可公开访问。

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. 您将不得不使用服务器端语言,如PHP,ASP.NET,node.js等,并创建一个“代理”,它将获得所需的文件作为参数,读取服务器上的文件,并发送其内容。

For example, in ASP.NET you can have such code: 例如,在ASP.NET中,您可以拥有以下代码:

Download.aspx 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:

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

You can't load from your PC like c:... You only can load file, from your server. 您无法从PC加载,例如c:...您只能从服务器加载文件。 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 如果这个html文件和test.html文件在服务器上的同一目录下,可以轻松加载test.html,如果它在另一个目录中,那么使用目录名,如test / test.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM