![](/img/trans.png)
[英]How to load a local html file inside an iframe in my react application?
[英]Load local html on iframe
我的服務器.html
頁面位於c:\\test\\test.html
。
我想顯示test.html
內的客戶機上的iframe,我該怎么辦呢?
我嘗試了什么:
<iframe id="serviceFrameSend" src="file:///c:\test\test.html" width="1000" height="1000" frameborder="0">
但它在客戶端機器上找到了test.html
文件,那么如何才能使test.html
從服務器加載?
如果不可能,我怎么能以另一種方式做到這一點?
當您在服務器上有頁面時,您需要使用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">
如果test.html
與主頁位於同一目錄中 ,則可以使用這些。 (主頁是帶有iframe的html。)
<iframe id="serviceFrameSend" src="../test.html" width="1000" height="1000" frameborder="0">
如果test.html
位於主頁的父目錄中,只要它不超出Web服務器的“root”目錄,就可以使用它。
<iframe id="serviceFrameSend" src="../test/test.html" width="1000" height="1000" frameborder="0">
如果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">
或者,您可以始終使用絕對路徑 ,基於test.html
的完整URL。
您必須將“test.html”文件放在服務器上的公共目錄中。 或者使“test”目錄可公開訪問。
您將不得不使用服務器端語言,如PHP,ASP.NET,node.js等,並創建一個“代理”,它將獲得所需的文件作為參數,讀取服務器上的文件,並發送其內容。
例如,在ASP.NET中,您可以擁有以下代碼:
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>
然后有這樣的iframe:
<iframe id="serviceFrameSend" src="Download.aspx?id=1" width="1000" height="1000" frameborder="0"></iframe>
您無法從PC加載,例如c:...您只能從服務器加載文件。 如果這個html文件和test.html文件在服務器上的同一目錄下,可以輕松加載test.html,如果它在另一個目錄中,那么使用目錄名,如test / test.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.