【问题标题】:How to download a complete html code with .html format? [duplicate]如何下载 .html 格式的完整 html 代码? [复制]
【发布时间】:2015-11-20 00:11:34
【问题描述】:

您好,我正在创建一个动态 html 部分,我有两个按钮:previewdownload

预览版运行良好。至于第二个按钮,即download,我想让按钮的动作使得如果单击download按钮,它会触发下载所有存储的html代码在变量rohit 中使用.html 格式。

我该怎么做?请告诉我。我在谷歌上搜索过,但找不到方法。

$(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 .
        });
});
<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>

Jsfiddle Link here

【问题讨论】:

  • html文件是否保存在你的服务器中?
  • 没有保存到本地
  • 谢谢先生。 @Raghavendra 我的问题是解决这个 url 你在聊天中 ping 我谢谢这个..

标签: javascript jquery html


【解决方案1】:

简而言之:您需要使用Blob

欲了解更多信息和演示:

Save/download data generated in JavaScript.
Demo.

【讨论】:

    【解决方案2】:

    您最有可能要做的是将您请求的标头更改为:

    Content-Type: application/octet-stream
    Content-Disposition: attachment; filename="file.html"
    

    这将告诉浏览器下载您的文件。

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2020-11-22
      • 1970-01-01
      • 2013-05-12
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多