简体   繁体   English

使用Javascript进行“内容处理”行为

[英]“Content-disposition”-like behavior with Javascript

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? 我想知道是否有可能使浏览器的行为与通过仅使用客户端javascript看到“Content-disposition:attachment; filename = ...”时的行为相同? This implies the data for the file to be saved is available on the client side only. 这意味着要保存的文件的数据仅在客户端可用。

Ie suppose we have a javascript Array, on client side only, 即假设我们有一个javascript数组,仅在客户端,

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(ie 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. 如果这个问题没有简单的答案 - 非常感谢任何想法,谷歌关键字或链接。

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

var YourTextData = "text data here";
window.location.href = "data:application/octet-stream," + encodeURIComponent(YourTextData);

You can't do it with a pure JavaScript solution - you need to have the file push come from the server. 您无法使用纯JavaScript解决方案 - 您需要让文件推送来自服务器。

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. 但是,如果不告诉我们您正在使用的服务器端技术,您将无法获得有关如何执行此操作的更详细答案。

AFAIK, you cannot do this well using Javascript in any sort of cross-browser manner. AFAIK,你不能以任何形式的跨浏览器方式使用Javascript。

If you really don't want to use a server, you could make a hybrid solution using Flash. 如果您真的不想使用服务器,可以使用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. 基本上,您可以通过Javascript(ExternalInterface)创建自定义Flash控件,然后Flash控件将启动“文件保存”操作。

Here's an article on the topic: Save a file locally with Flash Player 10 这是一篇关于该主题的文章: 使用Flash Player 10在本地保存文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM