如何将当前文档的innerHTML作为文件下载? [英] How to download the current document's innerHTML as a file?
本文介绍了如何将当前文档的innerHTML作为文件下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法可以以编程方式
Is there a way I could download the current document's innerHTML
as file programmatically?
我没有成功进行以下尝试。它确实下载了当前文档的来源,但这并不是我正在寻找的,因为我想保留任何后加载文档修改。
I've made the following attempt without success. It does download the current document's source, however that's not what I am looking for, since I want to preserve any post-load document modifications.
var save = document.createElement('a');
save.href = "my location href.attr";
save.target = '_blank';
save.download = fileName || 'unknown';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
推荐答案
也许这样吗?然而,这可能不是很多的浏览器,但它可以在Chrome中使用。
Perhaps something like this? It's probably not very cross-browser however, but it works in Chrome.
function downloadCurrentDocument() {
var base64doc = btoa(unescape(encodeURIComponent(document.documentElement.innerHTML))),
a = document.createElement('a'),
e = new MouseEvent('click');
a.download = 'doc.html';
a.href = 'data:text/html;base64,' + base64doc;
a.dispatchEvent(e);
}
downloadCurrentDocument();
这篇关于如何将当前文档的innerHTML作为文件下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文