我可以使用jQuery来创建一个文件(和它的内容)动态? [英] Can I use jQuery to create a file (and its content) dynamically?

查看:121
本文介绍了我可以使用jQuery来创建一个文件(和它的内容)动态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的HTML code,

Here is my HTML code,

<ul>
  <li> Download <a href="#">file1</a> </li>
  <li> Download <a href="#">file2</a> </li>
  <li> Download <a href="#">file3</a> </li>
</ul>

我想创造的东西像的href = FILENAME.TXT。该文件名的文本文件,其内容需要动态地根据其标签被点击创建。

I want to create something like "href=filename.txt". The "filename" of the text file and its content needs to be created dynamically based on which tag was clicked.

我使用Web API获取JSON格式的数据,因此认为安全不应该是一个问题。

I use a web API to get data in JSON format and hence believe security should not be an issue.

推荐答案

由于 HTML5 ,您可以使用的 数据:网​​址和的 下载属性。例如,

As of HTML5, you can use a combination of data: URL and download attribute. For example,

<a href="data:text/plain;charset=UTF-8,Hello%20World!" download="filename.txt">Download</a>

或者编程,

<a id="programatically" href="#" download="date.txt">Download</a>

$("a#programatically").click(function() {
    var now = new Date().toString();
    this.href = "data:text/plain;charset=UTF-8," + encodeURIComponent(now);
});​

查看这里。

不幸的是, 下载属性不完全支持和<一HREF =htt​​p://caniuse.com/#feat=datauri> 数据:的URL是在良好的轨道

Unfortunately, download attribute is not fully supported and data: URLs are in good track.

现在,为了更好地跨浏览器支持,你需要在服务器端动态创建的文件。

Now, for a better cross-browser support you will need to create the file dynamically at server-side.

这篇关于我可以使用jQuery来创建一个文件(和它的内容)动态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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