简体   繁体   English

如何在iframe中显示本地文件夹中的html文件?

[英]How to display html file from local folder inside an iframe?

I try to display html file inside an iframe. 我尝试在iframe中显示html文件。

The html file is located inside a local folder on my server, with the relevant permissions (for example: c:\\temp ). html文件位于我服务器上的本地文件夹中,具有相关权限(例如: c:\\temp )。

I tried: 我试过了:

iframe = document.createElement("iframe");
iframe.src = "file:///c:\temp\a.html";
iframe.style.display = "block";
document.body.appendChild(iframe);

But the content is not displayed inside the iframe. 但内容不会显示在iframe中。

another solution I tried is to use ajax to display the html inside a div : 我尝试的另一个解决方案是使用ajaxdiv显示html:

$("#DIV").html( *READ HTML TEXT FROM FILE *)

But the problem here is that I have pictures and css file included inside the html page, and they can't be displayed because the current location is my web site (they are included in c:\\temp folder with the html). 但问题是我在html页面中包含了图片和css文件,因为当前位置是我的网站(它们包含在带有html的c:\\temp文件夹中),所以无法显示它们。

Does anyone have an idea how can I display html page with images inside an iframe ? 有谁知道如何在iframe显示包含图像的html页面?

The problem is that the browser sees that file:///c:\\temp\\a.html as a file local to the browser, so it points to the C: drive of the visitor. 问题是浏览器将该file:///c:\\temp\\a.html视为浏览器的本地文件,因此它指向访问者的C:驱动器。

You can set up that local folder to be a Virtual Directory of your website (in IIS) and refer to the file using the correct web-path (http://...). 您可以将该本地文件夹设置为您网站的虚拟目录(在IIS中),并使用正确的Web路径(http:// ...)引用该文件。 That way you don't move the folder but it is still available through the webserver. 这样您就不会移动文件夹,但仍然可以通过网络服务器获取。

Assuming it works properly when you load the html file from the root instead of temp (thus, ruling out everything except an issue with the file location), you can load a file relative to your website as long as you have access. 假设从root用户而不是temp加载html文件时它可以正常工作(因此,排除除文件位置问题以外的所有内容),只要您有权访问,就可以加载相对于您网站的文件。 It does require understanding your path(s) - both local and server, if applicable. 它确实需要了解您的路径 - 本地和服务器(如果适用)。 For instance, I have a folder called 'temp' on a hosted server that is a peer to my website. 例如,我在托管服务器上有一个名为“temp”的文件夹,它是我网站的同行

Using Request.PhysicalApplicationPath, I find that my site is located here on my host: "c:\\inetpub\\wwwroot\\mydomain.com\\index.htm”. Therefore my temp folder & file must be at “c:\\inetpub\\wwwroot\\temp\\myfile.txt" (again, temp is a peer to my site). 使用Request.PhysicalApplicationPath,我发现我的站点位于我的主机上:“c:\\ inetpub \\ wwwroot \\ mydomain.com \\ index.htm”。因此我的临时文件夹和文件必须位于“c:\\ inetpub \\ wwwroot \\ temp \\ myfile.txt“(再次,temp是我网站的同行)。

Crude Example: 原油示例:

string _filePath, _uriServerName = "";

_filePath = Request.PhysicalApplicationPath;
_uriServerName = Request.ServerVariables["SERVER_NAME"].ToLower();

if (_uriServerName != "localhost")
{
int rootPos = _filePath.IndexOf("wwwroot"); //get position wwwroot 
_filePath = _filePath.Substring(0, rootPos) + "temp\\myfile.txt";
}

I use the above for loading files outside web site. 我使用上面的方法来加载网站外的文件。 For that, it works well. 为此,它运作良好。 However, an iframe may present some other challenges. 但是,iframe可能会带来一些其他挑战。 I don't believe this should present any cross-domain issues, but I'm not perfectly sure. 我不相信这会出现任何跨域问题,但我不完全确定。

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

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