如何使用js将浏览器中的内容保存到本地? [英] How to use js to save the content in the browser to local?

查看:299
本文介绍了如何使用js将浏览器中的内容保存到本地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Javascript生成一个txt文件,其内容将是一个String并将其链接到一个按钮,以便按钮单击将生成文件下载。该文件无法存储在任何地方,因为内容应该是动态的。

I'd like to generate a txt file using Javascript whose contents would be a String and link to it to a button so that the button click would generate the file download. The file cannot be stored anywhere as the contents are supposed to by dynamic.

我该怎么做?

推荐答案

您无法使用javascript生成文件。

You cannot generate a file using javascript.

但是,您可以使用隐藏的iframe链接到一个PHP文件。该PHP文件应包含正确的标头( Content-Type Content-Disposition 等)。

What you can do however, is do it with a hidden iframe that links to a PHP file. That PHP file should contain the proper headers (Content-Type, Content-Disposition, etc).

您可以在此处找到更多详细信息:通过ajax调用php 下载文件

You should find more details here: Download file through an ajax call php

这篇关于如何使用js将浏览器中的内容保存到本地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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