如何让浏览器提示用户保存服务器动态生成的文件?

时间:2023-02-15 00:28:31

Clients submit post requests using jquery ajax as illustrated below:

客户端使用jquery ajax提交post请求,如下所示:

$.ajax({
    url: "/xxx?request1", 
    data: theParams,
    type: 'post',
    error: function(XMLHttpRequest, textStatus, errorThrown){
        // error handling
    },
    success: function(data){
    var theResult = JSON.parse(data);
        // success handling
    }
});

Apache is configured to pass requests with /xxx? to a custom app. The app processes the request and returns information. Usually, this information returned as JSoN and is displayed in the "success handling" section, using jquery/javascript. The app writes the following headers (which are then returned via apache) prior to writing the JSON:

Apache配置为使用/ xxx传递请求?到自定义应用程序。应用程序处理请求并返回信息。通常,此信息以JSoN形式返回,并使用jquery / javascript显示在“成功处理”部分中。在编写JSON之前,应用程序会编写以下标头(然后通过apache返回):

HTTP/1.0 200 OK
Connection: close
Server: MyServer v. 0.826
Content-Type: text/html

I also have cookies working by placing the following right after the 200 OK line:

我也有饼干工作,在200 OK线之后放置以下内容:

Set-Cookie: cookie_name=value; Domain=example.com; Path=/; Expires=Fri, 21-Mar-2014 01:40:22 GMT; HttpOnly

In one scenario, I'd like the user to be able to save the returned data (as a plain text file). But here is where I run into problems.

在一种情况下,我希望用户能够保存返回的数据(作为纯文本文件)。但这是我遇到问题的地方。

If I use JavaScript to open a window and write the data to the new window, I've found that Safari and Chrome both disable the "Save as..." menu option (on Macs).

如果我使用JavaScript打开一个窗口并将数据写入新窗口,我发现Safari和Chrome都禁用了“另存为...”菜单选项(在Mac上)。

(I have got a version working by using base64 URI, but that looks unpolished relative to the rest of the site.)

(我有一个使用base64 URI工作的版本,但相对于网站的其他部分,它看起来没有抛光。)

I then began trying to include a Content-Disposition: attachment; filename=file.txt header but I could never get the browser to produce a prompt. I tried placing this header right after the 200 OK line, as well as later:

然后我开始尝试包含内容处理:附件; filename = file.txt标题,但我永远无法让浏览器生成提示。我尝试将此标题放在200 OK行之后,以及之后:

Content-Type: text/plain
Content-Disposition: attachment; filename=file.txt

In addition to being not sure of the exact format of the Content-Disposition header, I'm also not sure if I have the Content-Type correct or if there's something in the ajax handler code that is preventing the save.

除了不确定Content-Disposition标头的确切格式之外,我还不确定我是否正确使用Content-Type,或者ajax处理程序代码中是否存在阻止保存的内容。

I've tried to find examples where everything is put together, but can only find parts and clearly I can't get it put together correctly. Any suggestions?

我试图找到将所有东西放在一起的例子,但只能找到部件而且显然我无法正确地将它放在一起。有什么建议么?

I'm targeting only modern browsers, three major platforms (Mac/Windows/Linux), and only HTML5/JavaScript on the client side.

我的目标只是现代浏览器,三个主要平台(Mac / Windows / Linux),客户端只有HTML5 / JavaScript。

1 个解决方案

#1


8  

Content-Disposition: attachment is the right header, but the browser only obeys it when loading the file directly. Using AJAX (even through jQuery) will not work.

Content-Disposition:附件是正确的标题,但浏览器只在直接加载文件时才服从它。使用AJAX(甚至通过jQuery)将无法正常工作。

There is a way to do this. Read the full explanation in this answer, but basically you just have to write that in your success function:

有一种方法可以做到这一点。阅读本答案中的完整说明,但基本上你只需要在你的成功函数中写下:

window.location = "data:application/octet-stream," + encodeURIComponent(theResult);

Edit: FileSaver.js provides a wrapper improving browser compatibility and adding support for filenames for compatible browsers:

编辑:FileSaver.js提供了一个包装器,提高了浏览器兼容性,并为兼容的浏览器添加了对文件名的支持:

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");

#1


8  

Content-Disposition: attachment is the right header, but the browser only obeys it when loading the file directly. Using AJAX (even through jQuery) will not work.

Content-Disposition:附件是正确的标题,但浏览器只在直接加载文件时才服从它。使用AJAX(甚至通过jQuery)将无法正常工作。

There is a way to do this. Read the full explanation in this answer, but basically you just have to write that in your success function:

有一种方法可以做到这一点。阅读本答案中的完整说明,但基本上你只需要在你的成功函数中写下:

window.location = "data:application/octet-stream," + encodeURIComponent(theResult);

Edit: FileSaver.js provides a wrapper improving browser compatibility and adding support for filenames for compatible browsers:

编辑:FileSaver.js提供了一个包装器,提高了浏览器兼容性,并为兼容的浏览器添加了对文件名的支持:

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");