【问题标题】:Download and Load HTML file in iFrame在 iFrame 中下载和加载 HTML 文件
【发布时间】:2019-06-29 09:05:20
【问题描述】:

我正在尝试创建一个文件/文件夹查看器并下载我将在网页上显示网络共享目录的位置,它将显示共享的所有内容。如果文件是 pdf 或 xls 或 doc,它将被下载,如果它是 HTML,那么它将显示在页面上的 iFrame 中。

我已经完成了第一件事(下载文件),但无法完成第二部分;在 iFrame 中显示 HTML 文件。

当我单击 HTML 文件时,它会像其他文件一样下载,我不确定如何不下载 HTML 文件而是显示在屏幕上。

这个想法是将目录显示为 KB,其中放置了常见问题解答文档或页面。

这是我的下载代码。

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

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

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();
}

【问题讨论】:

    标签: javascript html asp.net ajax


    【解决方案1】:

    也许你不应该将这个$("#iframe").attr("src", "/dl.aspx?file=" + file); 用于 HTML 文件。通过这样做,您可以调用“想要”下载文件的 dl.aspx 文件。

    相反,请检查:

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

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

    【讨论】:

    • 您的建议给了我一个想法,我能够实现该功能。我现在正在做的是将 .html 文件复制到服务器上的目录并在 iframe 上设置该 URL 并从那里显示文件。
    • 当它是一个 .html 文件时,我执行 ajax 调用并获取路径并设置为 iframe。 [WebMethod] public static string GetPath(string name) { string fps = name; string fpd = Path.Combine(HttpContext.Current.Server.MapPath("~/temppage"), Path.GetFileName(fps)); File.Copy(fps, fpd, true); return "/temppage/" + Path.GetFileName(fpd); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多