Javascript 的“内容处置”行为

发布于 2024-10-30 18:50:27 字数 502 浏览 5 评论 0原文

我想知道是否可以仅使用客户端 JavaScript 使浏览器的行为与看到“Content-disposition:attachment;filename=...”时的行为相同?这意味着要保存的文件的数据仅在客户端可用。

即假设我们有一个 javascript 数组,仅在客户端,

var data = [
              ["aa","bb","cc","dd","ee","ff","gg","hh","ii"]
              [ 1,   2,   3,   4,   5,   6,   7,   8,   9],
              ..
           ];

我想将此数组作为文本文件保存到用户的计算机上。必须提示用户输入文件名(即我不是试图绕过浏览器的安全设置或类似的东西)。

如果不将数组存储到服务器的临时文件并发出另一个请求将此临时文件返回给用户,这是否可行? 如果这个问题没有简单的答案 - 任何想法、谷歌关键字或链接都非常感激。

I was wondering if it is possible to make browser behaving the same way as it does when sees "Content-disposition: attachment; filename=..." by using client-side javascript only? This implies the data for the file to be saved is available on the client side only.

I.e. suppose we have a javascript Array, on client side only,

var data = [
              ["aa","bb","cc","dd","ee","ff","gg","hh","ii"]
              [ 1,   2,   3,   4,   5,   6,   7,   8,   9],
              ..
           ];

and I want to save this array as a text file to the users's computer. The user must be prompted for the file name(i.e. I'm NOT trying to bypass browser's security settings or anything like that).

Is this doable without storing array to the server's temp file and making another request to get this temp file back to the user?
If there is no simple answer to this question - any ideas, google keywords or links are much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

懒的傷心 2024-11-06 18:50:28

您无法使用纯 JavaScript 解决方案来做到这一点 - 您需要从服务器推送文件。

但您不必在服务器上拥有“物理”文件即可完成此操作。您可以将数据字符串存储在内存中并从内存写入响应流。但是,如果不告诉我们您正在使用哪种服务器端技术,您将无法获得有关如何执行此操作的更详细的答案。

You can't do it with a pure JavaScript solution - you need to have the file push come from the server.

But you don't have to have a "physical" file on the server to accomplish that. You can store your data string in memory and write to the response stream from memory. But you won't get any more detailed answer on how to do that without telling us what server-side technology you're working with.

顾铮苏瑾 2024-11-06 18:50:28

AFAIK,你不能以任何跨浏览器的方式使用 Javascript 来很好地做到这一点。

如果您确实不想使用服务器,您可以使用 Flash 制定混合解决方案。本质上,您将创建一个自定义 Flash 控件,您将通过 Javascript(外部接口)与其进行通信,然后 Flash 控件将启动“文件保存”操作。

以下是有关该主题的文章:使用 Flash Player 10 在本地保存文件

AFAIK, you cannot do this well using Javascript in any sort of cross-browser manner.

If you really don't want to use a server, you could make a hybrid solution using Flash. Essentially you would make a custom Flash control that you would communicate with via Javascript (ExternalInterface) and then the Flash control would initiate the "file save" operation.

Here's an article on the topic: Save a file locally with Flash Player 10

空心↖ 2024-11-06 18:50:28

看看dowloadify:它使用@Adam建议的技术

Take a look at dowloadify: it uses the technique suggested by @Adam

就此别过 2024-11-06 18:50:28
var YourTextData = "text data here";
window.location.href = "data:application/octet-stream," + encodeURIComponent(YourTextData);
var YourTextData = "text data here";
window.location.href = "data:application/octet-stream," + encodeURIComponent(YourTextData);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文