【问题标题】:Why .txt file in <iframe> is getting downloaded instead of displayed?为什么 <iframe> 中的 .txt 文件被下载而不是显示?
【发布时间】:2015-08-31 21:01:10
【问题描述】:

我正在使用&lt;iframe&gt; 来显示一个文本文件:

<div class="document-view">
    <img src="img/302.GIF" />
</div> 

$(window).load(function () {
    <s:if test="extention.equalsIgnoreCase('txt')">
      element = '<iframe class="iframe" src="/dmsrepo/<s:property value="docLocation"/>" />';
    </s:if>

    $('.document-view').html(element);
});

当我在浏览器中检查元素时,我可以看到文件位置。

<iframe class="iframe" src="/dmsrepo/Legal Doc Type/LegalDocType_123456789_1.0.txt" />

但文本文件正在 Chrome、Firefox 和 IE 中下载。

如何解决这个问题?


编辑:您可以在第一个页面加载之后重现以下小提琴中的行为,奇怪的是只影响 Firefox。

只需open the page,然后按运行

注意:如果 Firebug Net 模块被激活,它也会影响第一次加载。

【问题讨论】:

  • @AndreaLigios Nope..仍在下载文件..
  • @AndreaLigios 你是对的..当我在 Firefox 中刷新页面时下载它。但在 IE 或 chrome 中没有问题。

标签: html firefox iframe download struts2


【解决方案1】:

显示文件在 .htaccess 中添加以下行

AddType text/plain .txt
AddType text/plain .log

这是 Firefox 的信号,您不必下载文件而是以纯文本形式查看文件。此技术可以应用于您需要查看而不是下载的所有文件。

【讨论】:

    【解决方案2】:

    这是文件的问题。它没有格式化为 html。因为它在文件读取的末尾有一些特殊的 unicode (eg) 字符。如果文本响应具有这样的特殊字符。它无法将响应解析为嵌入在 iframe 中的 html。

    你可以看看这个例子:

    &lt;iframe src='http://humanstxt.org/humans.txt' /&gt; &lt;/iframe&gt; 

    【讨论】:

      【解决方案3】:

      因为浏览器只能理解 html。将文件扩展名更改为 .html
      或者使用像php这样的服务器端语言并使用file_get_contents()函数,你可以向浏览器显示文本文件。

      【讨论】:

      • 那为什么它可以在 Chrome、IE 等上运行呢?也是第一次在 Firefox 上?
      • 好吧,+1 因为最后一个链接。 @prince 尝试将 .txt 文件重命名为 .html 并查看它是否也适用于 Firefox
      • 我尝试更改扩展名,但 html 视图不包含任何换行符。因此它不会保持文本文件的结构。所以我认为我应该将其作为 html 文件使用 br> 在业务逻辑中。
      猜你喜欢
      • 2018-11-10
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 2020-12-26
      • 2013-12-02
      相关资源
      最近更新 更多