![](/img/trans.png)
[英]How to make a browser display a "save as dialog" so the user can save the content of a string to a file on his system?
[英]HTTP headers to make browser show “save as” dialog and save file
在我的FCGI应用程序中,我希望以某种方式使服务器端响应,以使浏览器(要使用大多数浏览器)打开“另存为”对话框,然后将文件实际保存在用户硬盘上。 我失败了(这是从Chrome接收到的请求/响应的转储:
Remote Address:192.168.1.69:80
Request URL:http://192.168.1.69/sunprint/sunweb.fcgi?GETPCBSDATAASFILE2SAVE
Request Method:GET
Status Code:200 OK
Request Headers
GET /sunprint/sunweb.fcgi?GETPCBSDATAASFILE2SAVE HTTP/1.1
Host: 192.168.1.69
Connection: keep-alive
Accept: */*
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) >Chrome/34.0.1847.116 Safari/537.36
Referer: http://192.168.1.69/sunprint/PCBsVersions.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Query String Parameters
GETPCBSDATAASFILE2SAVE
Response Headers
HTTP/1.1 200 OK
Date: Mon, 05 May 2014 10:21:23 GMT
Server: Apache/2.2.22 (Ubuntu)
Cache-Control: no-cache, must-revalidate
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Content-Description: File Transfer
Content-Disposition: attachment; filename="SunSerialNumbers.txt"
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 227
Keep-Alive: timeout=5, max=81
Connection: Keep-Alive
Content-Type: text/plain
我文件的内容是一组可打印的ASCII符号。 顺便说一句,我应该以某种方式对内容进行编码吗?
似乎所有必需的标头都存在,但浏览器仍然拒绝显示所需的对话框。 标头组合是否存在错误?
要发出请求,我使用以下代码:
函数sendCommandGetFile(url1){
$ .ajax({
网址:url1,
类型:“ GET”
});
}sendCommandGetFile(“ sunweb.fcgi?GETPCBSDATAASFILE2SAVE”,function(data){});
非常感谢您的帮助。
X-Requested-With: XMLHttpRequest
没有一组HTTP标头会导致浏览器下载它响应使用XHR的请求而获取的文件。
您有三个基本选项。
location
设置为该URI data:
方案URI,并让客户端JS将其分配给location
。 除非您确实确实有时需要返回文件,有时需要返回数据以供JS处理(例如错误消息),否则选项1是最好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.