我正在创建一个动态html
部分,我有两个按钮:预览和下载。
预览很好用。至于第二个按钮,即下载,我想对按钮进行操作,以便在单击下载按钮时触发下载所有存储的html代码在变量rohit
中,格式为.html
。
我该怎么做?请告诉我。我曾经在Google上搜索过它,但找不到办法。
$(document).ready(function(){
var rohit = "<h2>Hello i m Dynamic Heading </h2>";
$('.preview').on('click', function(){
var myWindow = window.open('', "rohit", "width=800, height=400", '_blank');
myWindow.document.write(rohit);
});
$('.download').on('click', function(){
alert(rohit);
// here download code with html format how to do this .
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<button class="preview">click to preview</button>
<button class="download">CLick to Download</button>
&#13;
和 Jsfiddle Link here
答案 0 :(得分:1)
简而言之:您需要使用Blob
。
答案 1 :(得分:1)
您很可能需要做的是将请求的标题更改为:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="file.html"
这将告诉浏览器下载您的文件。