使用纯粹的javascript创建下载提示 [英] Creating download prompt using purely javascript

查看:83
本文介绍了使用纯粹的javascript创建下载提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在当前窗口的javascript变量中有一些文本数据(比如 var a ='Hello World From Javascript'; )。我想通过javascript执行以下
-

I have some text data (say var a = 'Hello World From Javascript';)in javascript variable in current window. I want to do the following through javascript-

1. open a new window and write the text data to the window.
2. set the content type to text/plain.
3. set the content-disposition to attachment, so that download prompt comes.
4. user downloads the text data as a text file and saves it to his local disk.

这一切都可以通过javascript实现吗?

is this all possible through javascript?

我知道我们可以对服务器或重定向进行ajax调用,但在这种情况下,而不是按照上述步骤。但在这种情况下,这些变通办法不具备适应性。

I know we can make ajax calls to server or redirect but in this case instead of following above steps. But in this case, these workarounds are not adaptable.

推荐答案

在没有任何服务器请求的情况下触发文件下载



不幸的是,这不是普通浏览器功能所能做到的。像flash或特定浏览器这样的插件可以满足您的需求,但javascript中的安全限制不允许您下载在浏览器中创建的任意数据。

Triggering a file download without any server request

Unfortunately this is not something you can do with normal browser capabilities. Something like flash or a browser-specific plugin will get you what you need, but security limitations within javascript will not let you download arbitrary data created within the browser.

此外,所有浏览器/版本组合均不支持data网址。我不确定您的用户是否受限于他们使用的是什么浏览器,但这可能会限制您可以使用该解决方案执行的操作。

Also the 'data' url is not supported across all browser/version combinations. I am not sure if your users are constrained on what browser they are using or not but that may limit what you can do with that solution.

这篇关于使用纯粹的javascript创建下载提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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