仅使用javascript生成和保存/下载文件 [英] Generating and saving/downloading files with only javascript

查看:61
本文介绍了仅使用javascript生成和保存/下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的游戏制作了一个地图编辑器,代码生成一个id为code的div。我想知道如何使用此代码生成javascript文件。显然我必须得到代码div的文本。 var code = $(#code)。text(); 将起作用。但现在我想用这段代码生成和下载javascript文件。甚至可以用javascript生成/下载文件,还是必须使用php?

I made a map editor for my game, the code is generated to a div with id "code". I am wondering how to generate a javascript file with this code. Obviously i have to get the text of the code div. var code = $("#code").text(); will work. But now i want to generate and download javascript file with this code. Is it even possible to generate/download a file with only javascript or do i have to use php?

我想手动选择文件夹和名称。

I want to choose the folder and the name manually.

推荐答案

对你的问题的简短回答是否定的。出于安全考虑,浏览器非常小心,不允许Javascript对文件系统做任何事情。

The short answer to your question is no. For security reasons browsers are very careful to NOT allow Javascript to do pretty much anything with the filesystem.

然而Flash没有这样的限制;退房: https://github.com/dcneiner/Downloadify 。当然,Flash有其自身的问题,例如移动浏览器往往不支持它(但由于您的用例正在下载文件,而移动用户可能不会这样做,这可能不是问题你知道。

Flash however has no such limitations; check out: https://github.com/dcneiner/Downloadify. Of course, Flash has its own issues, such as the fact that mobile browsers tend to not support it (but since your use case is downloading a file, and mobile users probably won't be doing that, this might not be an issue for you).

除了Downloadify(或类似的基于Flash的解决方案)之外,你唯一的选择就是Java小程序(你不想走这条路),浏览器插件,或某种服务器参与(例如@Brad Christie描述)。

Other than Downloadify (or a similar Flash-based solution) your only options are Java applets (you don't want to go down that road), browser plug-ins, or some sort of server-involvement (eg. as @Brad Christie described).

这篇关于仅使用javascript生成和保存/下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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