Javascript Blob document with html tags to be saved with formatting

て烟熏妆下的殇ゞ 提交于 2019-12-24 16:27:57

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/34389649/javascript-blob-document-with-html-tags-to-be-saved-with-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!