Javascript 在 iframe 上加载本地 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42393063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:06:57  来源:igfitidea点击:

Load local html on iframe

javascripthtmliframe

提问by CSharpBeginner

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

我的服务器.html页面位于c:\test\test.html.

I want to display test.htmlinside 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.htmlfile on the client machine, so how can I make that the test.htmlwill be loaded from the server?

但是它test.html在客户端计算机上找到了该文件,那么我如何才能test.html从服务器加载该文件?

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

如果不可能,我怎么能以另一种方式做到这一点?

回答by DomeTune

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

由于您的服务器上有页面,因此您需要使用http://url,而不是file:///. 这有点棘手,因为 Web 服务器的文件路径语法与您的文件系统不同。这里有一些选项。



<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.htmlis in the same directoryas your main-page. (The main-page is the html with the iframe in it.)

如果主页test.html位于同一目录中,则可以使用它们。(主页是带有 iframe 的 html。)



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

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

如果test.html它位于主页的父目录中,则可以使用它,只要它不超出 Web 服务器的“根”目录。



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

You can use this if the test.htmlis in the sibling directoryof your main-page, such as path/views/test/test.htmlwhen your main-page is path/views/main/page.html.

如果 位于主页test.html同级目录中,例如path/views/test/test.html当您的主页是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.

或者,您始终可以使用绝对路径,基于test.html.

回答by Tej Patil

You will have to place "test.html" file the public directory on the server. Or else make "test" directory publicly accessible.

您必须将“test.html”文件放在服务器上的公共目录中。或者使“测试”目录可公开访问。

回答by Shadow Wizard is Ear For You

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

下载.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>

回答by Tihi

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

您不能像 c:... 从您的 PC 加载文件,您只能从您的服务器加载文件。如果此 html 文件和 test.html 文件在您的服务器上的同一目录中,请使用 test.html 轻松加载,如果它在另一个目录中,则使用目录名称,例如 test/test.html