简体   繁体   English

在iFrame中下载并加载HTML文件

[英]Download and Load HTML file in iFrame

I am trying to create a file/folder viewer and downloaded where I will be showing a network shared directory on a web page and it'll show all the contents of the share. 我试图创建一个文件/文件夹查看器并下载到该位置,我将在网页上显示网络共享目录,它将显示共享的所有内容。 If the file is a pdf or xls or doc it'll download and if its HTML then it'll show in an iFrame on the page. 如果文件是pdf或xls或doc,则将下载,如果其HTML,则将在页面上的iFrame中显示。

I have achieved the first thing (downloading the files) but not able to do the 2nd part; 我已经完成了第一件事(下载文件),但是无法完成第二部分。 showing the HTML file in iFrame. 在iFrame中显示HTML文件。

When I click the HTML file it downloads like other files and I am not sure how to not download for HTML but show on screen. 当我单击HTML文件时,它会像其他文件一样下载,并且我不确定如何不下载HTML而是显示在屏幕上。

The idea is to show the directory as KB where FAQ doc or pages are placed. 想法是将目录显示为放置FAQ文档或页面的KB。

This is my code for download. 这是我的下载代码。

Javascript 使用Javascript

 $('#container_id').fileTree({
    root: '<network path to load files from>',
    expandSpeed: 1000,
    collapseSpeed: 1000,
    multiFolder: true
}, function(file) {

    $("#iframe").attr("src", "/dl.aspx?file=" + file);
    $('#iframe').load();
});

HTML HTML

<div class="example">
    <h2>File List</h2>
    <div id="container_id" class="demo"></div>
    <iframe id="iframe" style="display:none;"></iframe>
</div>

CS CS

if (Request.QueryString["file"] != null)
{
    string actualFilePath = Request.QueryString["file"].ToString();
    HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
    string filename = Path.GetFileName(actualFilePath);
    String Header = "Attachment; Filename=" + filename;
    HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
    HttpContext.Current.Response.WriteFile(actualFilePath);
    HttpContext.Current.Response.End();
}

Maybe you souldn't use this $("#iframe").attr("src", "/dl.aspx?file=" + file); 也许您根本不使用此$("#iframe").attr("src", "/dl.aspx?file=" + file); for the HTML file. 用于HTML文件。 By doing this you call your dl.aspx file who "want" download the file. 这样,您可以称自己的dl.aspx文件“想要”下载该文件。

Instead, do a check : 相反,请检查一下:

  • if the file is <> HTML, then do $("#iframe").attr("src", "/dl.aspx?file=" + file); 如果文件是<> HTML,则执行$("#iframe").attr("src", "/dl.aspx?file=" + file);

  • if = HTML do $("#iframe").attr("src", "file_path/my-html-file.html"); 如果= HTML,则执行$("#iframe").attr("src", "file_path/my-html-file.html");

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

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