如何使浏览器显示“另存为对话框"?所以用户可以将字符串的内容保存到他系统上的文件中?

2022-01-24 00:00:00 save javascript savefiledialog

如何让浏览器显示另存为对话框",以便用户可以将字符串的内容保存到系统上的文件中?

How can I make a browser display a "save as dialog" so the user can save the content of a string to a file on his system?

例如:

var myString = "my string with some stuff";
save_to_filesystem(myString,"myString.txt");

结果如下:

推荐答案

万一有人还在疑惑...

In case anyone is still wondering...

我是这样做的:

<a href="data:application/xml;charset=utf-8,your code here" download="filename.html">Save</a>

不记得我的来源,但它使用了以下技术功能:

can't remember my source but it uses the following techniquesfeatures:

  1. html5下载属性
  2. 数据 URI 的

找到参考:

http://paxcel.net/blog/savedownload-file-using-html5-javascript-the-download-attribute-2/

从评论中可以看出,这不在

As you can gather from the comments, this does NOT work in

  1. Internet Explorer(但适用于 Edge v13 及更高版本)
  2. 迷你歌剧

http://caniuse.com/#feat=download

相关文章