【问题标题】:Javascript Blob document with html tags to be saved with formatting带有 html 标记的 Javascript Blob 文档以格式保存
【发布时间】:2015-12-21 05:47:54
【问题描述】:

我正在使用 javascript blob 以可下载的文档格式保存一些如下文本。

<p style='font-size:18px'>Hello</p>

下载完成后,我希望文档只显示格式化的“Hello”,不带任何 html 标记。在 ubuntu 中这非常有效。 但是当我在 windowsgoogle docs 中打开同一个文档时,我仍然会看到 html 标记。 有没有办法可以在 Blob 级别本身进行这种格式化。下面是我创建 blob 对象的方式。

var file = new Blob([val], {type: "octet/stream"});

感谢您对此的帮助。

【问题讨论】:

  • 操作系统无关紧要,发布您的代码。

标签: javascript html blob doc


【解决方案1】:

尝试将 Blobtype 调整为 "text/html" ,使用 URL.objectCreateURL() 作为文件对象引用进行下载

var val = "<div>abc</div>";

var file = new Blob([val], {
  type: "text/html"
});
// file object reference
var download = URL.createObjectURL(file);

var a = document.createElement("a");
a.href = download;
a.download = "file-" + new Date().getTime();
document.body.appendChild(a);
a.click()

【讨论】:

  • 我尝试了以下。但即便如此,使用谷歌文档我也看到了 html 标签 var a = document.getElementById("a"); var file = new Blob([val], {type: "text/html"}); a.href = URL.createObjectURL(文件);
  • @Indraja 请参阅更新后的 stacksn-ps。 "with google docs" 不确定 google docs 如何呈现 html
  • 文档为 .doc 格式。我尝试在 Windows 操作系统中打开它。我看到了 html 标签。在 Ubuntu 中,我看到没有 html 标签的格式化文本。是否需要为此设置其他 blob 参数?
猜你喜欢
  • 2020-08-02
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2017-07-11
  • 2012-09-15
相关资源
最近更新 更多