Javascript Blob文档与html标签保存格式 [英] Javascript Blob document with html tags to be saved with formatting

查看:95
本文介绍了Javascript Blob文档与html标签保存格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用javascript blob将以下文本保存为可下载的文档格式。

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

一旦发生下载,我希望文档只显示格式化的< Hello '没有任何html标记。在 ubuntu 中这个工作得很好。
但是,当我在 windows google文档中打开相同的文档时,我仍然可以看到html标签。
有没有一种方法可以在Blob级本身进行格式化。下面是Iam创建blob对象的方式。

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

感谢您对此的帮助。

解决方案

尝试调整 Blob 文本的类型 / html,使用 URL.objectCreateURL()作为文件对象参考下载



  var val =< div> abc< / div>; var file = new Blob([val],{type:text / html}); // file object referencevar download = URL.createObjectURL(file); var a = document.createElement(a); a.href = download; a.download =file-+ new Date()。getTime(); document.body.appendChild(a ); a.click() 


I am having some text like below to be saved in a downloadable doc format using javascript blob.

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

Once the download happens I want the doc to show just show formatted 'Hello' without any html tags. In ubuntu this works very well. But when I open the same doc in windows or google docs, I still see html tags. Is there a way where I can do this formatting at Blob level itself. Below is the way Iam creating blob object.

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

Appreciate your help on this.

解决方案

Try adjusting type of Blob to "text/html" , using URL.objectCreateURL() as file object reference for download

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

这篇关于Javascript Blob文档与html标签保存格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆